Task 2: Host the Sequential Workflow

Download sample

The class responsible for executing workflow is the WorkflowRuntime class. When you create a host application, you must create an instance of this class and pass in the type specification of your workflow by using the CreateWorkflow method. You can then call the Start method from the returned WorkflowInstance object to start running your workflow. Because a sequential workflow does not rely on an external event to run, it begins immediately.

In this task, modify the Windows Form application that you created in the previous task to start the workflow you created. No child activities were added to the workflow itself, so the workflow finishes without any processing.

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 then proceeding to the steps in the following section.

Hosting the Windows Workflow Runtime Engine

To host the Windows Workflow runtime engine

  1. Open the SimpleExpenseReport project and add references to System.Workflow.Runtime, System.Workflow.Activities, System.Workflow.ComponentModel, and to the ExpenseReportWorkflow project by clicking Project, then Add Reference.

    The assemblies System.Workflow.Runtime.dll, System.Workflow.Activities.dll, and System.Workflow.ComponentModel.dll are located under the .NET tab and the ExpenseReportWorkflow project is under the Projects tab.

  2. Open Form1.cs and add the following using statement to the top of the source file:

    using System.Workflow.Runtime;
    using System.Workflow.Activities;
    using System.Workflow.ComponentModel;
    using ExpenseReportWorkflow;
    
  3. In the MainForm class, declare a private WorkflowRuntime field named workflowRuntime and a private WorkflowInstance field named workflowInstance as shown in the following example:

    private WorkflowRuntime workflowRuntime = null;
    private WorkflowInstance workflowInstance = null;
    
  4. In the MainForm constructor, following the InitializeComponent method, create a new instance of the workflowRuntime object and call the StartRuntime method.

    this.workflowRuntime = new WorkflowRuntime();
    this.workflowRuntime.StartRuntime();
    

    This starts the runtime but does not execute any workflows until the application notifies it to start a workflow.

  5. After the call to the StartRuntime method, create a generic EventHandler of the WorkflowCompletedEventArgs type for the WorkflowCompleted event that is raised by the workflowRuntime object. Name it workflowRuntime_WorkflowCompleted.

    workflowRuntime.WorkflowCompleted +=
        new EventHandler<WorkflowCompletedEventArgs>
        (workflowRuntime_WorkflowCompleted);
    
  6. In the MainForm class, create a new method named workflowRuntime_WorkflowCompleted that takes a System.Object parameter named sender and a WorkflowCompletedEventArgs parameter named e.

    void workflowRuntime_WorkflowCompleted(object sender,
        WorkflowCompletedEventArgs e)
    {
    }
    

    This event handler can be used to pass values back to the host application after the current WorkflowInstance has completed.

  7. In the submitButton_Click method, create a local Type object equal to the Type of the workflow that you created in the previous task.

  8. Call the CreateWorkflow method that is defined on the workflowRuntime object, passing the Type object that you created in the previous step.

    The CreateWorkflow method returns a WorkflowInstance object. Use this object to set the workflowInstance variable.

  9. Call the Start method that is defined on the workflowInstance object to start the workflow.

    The following code example shows the completed implementation for the submitButton.Click event handler.

    private void submitButton_Click(object sender, EventArgs e)
    {
        Type type = typeof(ExpenseReportWorkflow.ExpenseReportWorkflow);
    
        // Start the workflow.
        this.workflowInstance = workflowRuntime.CreateWorkflow(type);
        this.workflowInstance.Start();
    }
    

    Whenever the Submit button is clicked in the Windows Form application, the sequential workflow runs. At this time, however, it exits because no child activities are currently present within the workflow.

  10. Press F5, or click Debug, and point to Start Debugging to compile the project.

In Exercise 2: Create the Expense Report Service of this tutorial, communication between the Windows Form and the workflow is enabled.

See Also

Reference

WorkflowRuntime
WorkflowInstance
CreateWorkflow
Start
WorkflowCompleted
WorkflowCompletedEventArgs

Other Resources

Exercise 2: Create the Expense Report Service
Hosting
Tutorial: Host the WF Runtime

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04