Passing Arguments

Intertech Tutorials

Passing Arguments to the WF Runtime Engine

Currently, the example workflow makes use of a number of hardcoded rules. For example, one rule dictates the maximum length of the input data and the error message to display. Ideally, you might like to specify arguments to the WF engine to avoid too much hardcoding. In this way, you can execute a WF using external parameters (for example, data obtained from a database or a *.config file).

The WF runtime supports the use of custom parameters using a generic Dictionary<string, object> type. The name / value pairs added to the Dictionary object must then be associated to identically named CLR properties on your workflow instance. Public fields are not allowed. Dictionary objects must map to a writable public property. To register these parameters with the WF runtime engine, pass in your Dictionary object as a second parameter to the CreateWorkflow() method.

Assume the following update to Main(), which defines a Dictionary<string, object> containing two data items. The first item is a string literal that represents the error message to display if the name is too long. The second is a numeric value that will be used to specify the maximum length of the user name. In a production application, these values would most likely be pulled in from an external source, such as a *.config file or a database. However, here you will simply specify fixed values.


// C#
using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
...
// Define two parameters for use by your workflow.
// Ideally, these would come from an external file.
Dictionary<string, object> parameters =
new Dictionary<string, object>();
parameters.Add("ErrorMessage", "Ack! Your name is too long!");
parameters.Add("NameLength", 5);

// Now pass in parameters.
WorkflowInstance instance = workflowRuntime.CreateWorkflow(
typeof(UserDataWFApp.ProcessUsernameWorkflow), parameters);

instance.Start();
waitHandle.WaitOne();
}



' VB
Using workflowRuntime As New WorkflowRuntime()
...
' Define two parameters for use by your workflow.
' Ideally, these would come from an external file.
Dim parameters As Dictionary(Of String, Object) _
= New Dictionary(Of String, Object)()
parameters.Add("ErrorMessage", "Ack! Your name is too long!")
parameters.Add("NameLength", 5)

' Now pass in parameters.
Dim workflowInstance As WorkflowInstance
workflowInstance = workflowRuntime.CreateWorkflow _
(GetType(Workflow1), parameters)

workflowInstance.Start()
WaitHandle.WaitOne()
End Using

You can now associate these input parameters to properties on your WF instance. Furthermore, you can remove the previous hardcoded values within your WF instance by using the property names. The VB code would be similar.


// C# (VB code would be similar)
public sealed partial class ProcessUsernameWorkflow:
SequentialWorkflowActivity
{
...
// These properties map to the incoming host arguments.
private string m_errorMsg;
private int m_nameLength;

public string ErrorMessage
{
get { return m_errorMsg; }
set { m_errorMsg = value; }
}
public int NameLength
{
get { return m_nameLength; }
set { m_nameLength = value; }
}

private void GetAndValidateUserName
(object sender, ConditionalEventArgs e)
{
Console.Write("Please enter name,
which much be less than {0} chars: ",
NameLength);
userName = Console.ReadLine();

// See if name is correct length and set the result.
e.Result = (UserName.Length > NameLength);
}

private void NameNotValid(object sender, EventArgs e)
{
Console.WriteLine(ErrorMessage);
}
}

Copyright (c) 2008-2013. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.