Task 1: Create a Workflow and Pass Parameters
When you run workflows, you have the option of passing data that is used for initialization through the use of parameters. This is done by creating a Dictionary-based collection that contains key-value pairs that directly map to workflow-defined properties and their associated values.
In this exercise, you modify the workflow that you created in Exercise 1: Build a Windows Console Host to accept three parameters. Two of the parameters correspond to two operands that are used for an addition operation, and the remaining parameter is used to store the result of that operation.
After you create the properties in the workflow and modify a CodeActivity activity to perform the addition operation, you modify the host application code to pass these parameters and output the result from the workflow.
Note
Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.
To define workflow properties for parameters
Create three private Int32 fields in the HostingWorkflows class.
private int op1; private int op2; private int opResult;
In the HostingWorkflows class, create a Int32 property named Operand1.
Additionally, create an accessor method in the property to assign the op1 variable.
public int Operand1 { set { this.op1 = value; } }
In the HostingWorkflows class, create an Int32 property named Operand2.
Additionally, create an accessor method in the property to assign the op2 variable.
public int Operand2 { set { this.op2 = value; } }
In the HostingWorkflows class, create a Int32 property named Result.
Additionally, create an accessor method in the property to return the value of the opResult variable.
public int Result { get { return this.opResult; } }
Modify the codeActivity1_ExecuteCode method that you defined in the HostingWorkflows class to add the op1 and op2 ** fields together, and place the result in the opResult field.
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { Console.WriteLine("In codeActivity1_ExecuteCode. Adding operands"); this.opResult = this.op1 + this.op2; }
To pass parameters from the host application
Define a new static Dictionary collection that uses String for keys and Object for values, and create an instance of that collection in the Program class.
static Dictionary<string, object> parameters = new Dictionary<string, object>();
In the Main method, after the StartRuntime method call, use ReadLine to query the user for two operands, and place these values in the Dictionary collection that you created in Step 1.
Note
Make sure that the keys that you use for the Dictionary collection correspond directly with the name of the properties that you defined in the previous procedure. For this example, these keys should be named Operand1 and Operand2. Additionally, this code uses error handling to ensure a user does not enter a value that is too large.
// retrieve workflow parameters do { try { Console.Write("Enter a value for operand1: "); parameters["Operand1"] = Int32.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine(e.Message + Environment.NewLine); } } while (parameters.ContainsKey("Operand1") == false); do { try { Console.Write("Enter a value for operand2: "); parameters["Operand2"] = Int32.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine(e.Message + Environment.NewLine); } } while (parameters.ContainsKey("Operand2") == false);
Modify the method call to CreateWorkflow by passing the parameters Dictionary collection as the second parameter.
WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(type, parameters);
Modify the workflowRuntime_WorkflowCompleted method to output the results of the addition that is performed in the workflow by using the WriteLine method.
The result of the addition is stored in the OutputParameters collection, which is defined in the WorkflowCompletedEventArgs parameter.
static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { Console.WriteLine("Result: {0}", e.OutputParameters["Result"]); waitHandle.Set(); }
Compiling the Code
For information about compiling your code, see Compiling the Code.
In Exercise 3: Work with Runtime Events, you learn how to subscribe to events that are raised by the Windows Workflow Foundation runtime engine.
See Also
Reference
OutputParameters
WorkflowCompletedEventArgs
CreateWorkflow
Other Resources
Exercise 3: Work with Runtime Events
Sequential Workflow With Parameters
Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04