Share via


Task 1: Create a Workflow and Pass Parameters 

Download sample

When you execute 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 will modify the host application code to pass these parameters and output the result from the workflow.

NoteNote

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.

Defining the Workflow Properties

To define workflow properties for parameters

  1. Create three private Int32 fields in the HostingWorkflows class.

    Private op1 As Integer
    Private op2 As Integer
    Private opResult As Integer
    
    private int op1;
    private int op2;
    private int opResult;
    
  2. In the HostingWorkflows class, create a Int32 property named Operand1.

    Additionally, create an accessor method in the property to assign the op1 variable.

    Public WriteOnly Property Operand1() As Integer
        Set(ByVal value As Integer)
            Me.op1 = Value
        End Set
    End Property
    
    public int Operand1
    {
        set
        {
            this.op1 = value;
        }
    }
    
  3. In the HostingWorkflows class, create an Int32 property named Operand2.

    Additionally, create an accessor method in the property to assign the op2 variable.

    Public WriteOnly Property Operand2() As Integer
        Set(ByVal value As Integer)
            Me.op2 = Value
        End Set
    End Property
    
    public int Operand2
    {
        set
        {
            this.op2 = value;
        }
    }
    
  4. 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 ReadOnly Property Result() As Integer
        Get
            Return Me.opResult
        End Get
    End Property
    
    public int Result
    {
        get
        {
            return this.opResult;
        }
    }
    
  5. 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 Sub codeActivity1_ExecuteCode(ByVal sender As Object, _
        ByVal e As EventArgs)
        Console.WriteLine("In codeActivity1_ExecuteCode. Adding operands")
        Me.opResult = Me.op1 + Me.op2
    End Sub
    
    private void codeActivity1_ExecuteCode(object sender, EventArgs e)
    {
        Console.WriteLine("In codeActivity1_ExecuteCode. Adding operands");
        this.opResult = this.op1 + this.op2;
    }
    

Passing Parameters from the Host Application

To pass parameters from the host application

  1. 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.

    Private Shared parameters As Dictionary(Of String, Object) = _
              New Dictionary(Of String, Object)()
    
    static Dictionary<string, object> parameters = new Dictionary<string, object>();
    
  2. 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.

    NoteImportant

    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 ex As Exception
            Console.WriteLine(ex.Message + Environment.NewLine)
        End Try
    Loop While parameters.ContainsKey("Operand1") = False
    
    Do
        Try
            Console.Write("Enter a value for operand2: ")
            parameters("Operand2") = Int32.Parse(Console.ReadLine())
        Catch ex As Exception
            Console.WriteLine(ex.Message + Environment.NewLine)
        End Try
    Loop While parameters.ContainsKey("Operand2") = False
    
    // 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);
    
  3. Modify the method call to CreateWorkflow by passing the parameters Dictionary collection as the second parameter.

    Dim workflowInstance As WorkflowInstance = _
                 workflowRuntime.CreateWorkflow(type, parameters)
    
    WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(type, parameters);
    
  4. 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.

    Private Shared Sub workflowRuntime_WorkflowCompleted _
              (ByVal sender As Object, ByVal e As WorkflowCompletedEventArgs)
        Console.WriteLine("Result: {0}", e.OutputParameters("Result"))
        waitHandle.Set()
    End Sub
    
    static void workflowRuntime_WorkflowCompleted(object sender, 
        WorkflowCompletedEventArgs e)
    {
        Console.WriteLine("Result: {0}", e.OutputParameters["Result"]);
        waitHandle.Set();
    }
    

Compiling the Code

  1. Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. Go to the source directory of the tutorial.

  3. At the command prompt, type MSBUILD to build the project.

In the next exercise, Exercise 3: Work with Runtime Events, you will 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

Footer image

Send comments about this topic to Microsoft.