Partilhar via


Task 3: Create a Workflow Host Console Application

Download sample

In the previous task, you created a sequential workflow that is used throughout the rest of this tutorial. In this step, you create a console application that is used to host the Windows Workflow Foundation runtime engine. The remaining exercises and tasks in this tutorial build on this hosting application by layering additional features that are found in Windows Workflow Foundation.

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 create the hosting class

  1. Create a new source file named Program.

    Give the file a .vb extension if you are creating a Visual Basic application or a .cs extension if you are creating a C# application.

  2. Add the following directives to import the types that you must have for the hosting application.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Workflow.Runtime;
    using System.Workflow.Runtime.Hosting;
    
  3. Create a new class named Program in the Microsoft.Samples.Workflow.Tutorials.Hosting namespace.

    Namespace Microsoft.Samples.Workflow.Tutorials.Hosting
        Class Program
        End Class
    End Namespace
    
    namespace Microsoft.Samples.Workflow.Tutorials.Hosting
    {
        class Program
        {
        }
    }
    
  4. In the Program class, create an AutoResetEvent field named waitHandle.

    This object is used to shut down the application when the workflow has finished running.

    static AutoResetEvent waitHandle = new AutoResetEvent(false);
    
  5. In the Program class, create the Main method that takes an array of String objects named args as a parameter.

    Shared Sub Main(ByVal args As String())
    End Sub
    
    static void Main(string[] args)
    {
    }
    
  6. Create a local variable named workflowRuntime of type WorkflowRuntime.

  7. Start the Windows Workflow Foundation runtime engine by calling the StartRuntime method that was defined in the workflowRuntime object.

  8. Add an event handler to the WorkflowCompleted event that was raised by the workflowRuntime object.

    WorkflowRuntime workflowRuntime = new WorkflowRuntime();
    workflowRuntime.StartRuntime();
    workflowRuntime.WorkflowCompleted += new
        EventHandler<WorkflowCompletedEventArgs>
        (workflowRuntime_WorkflowCompleted);
    
  9. Create a new Type object named type, and set its value equal to the Type for the sequential workflow that was created in the previous task.

  10. Call the CreateWorkflow method that was defined in the workflowRuntime object, passing the Type object that was retrieved in the previous step.

    This method returns a WorkflowInstance object that is used in the next step.

  11. Call the Start method that was defined in the WorkflowInstance object, which was created in the previous step.

    Type type = typeof(HostingWorkflows);
    WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(type);
    workflowInstance.Start();
    
  12. Call the WaitOne method that was defined in the waitHandle object.

    This method does not return until the workflow finishes running.

  13. Call the StopRuntime method that is defined in the workflowRuntime object.

    By calling this method, you make sure that the workflow runtime engine completes processing and shuts down properly.

    waitHandle.WaitOne();
    workflowRuntime.StopRuntime();
    
  14. Define a new static method named workflowRuntime_WorkflowCompleted in the Program class.

    This method takes a Object type named sender and a WorkflowCompletedEventArgs object named e as parameters.

  15. In the workflowRuntime_WorkflowCompleted method, call the Set method that is defined in the waitHandle object.

    When the workflow finishes running, the workflowRuntime_WorkflowCompleted event is raised. When the Set method on the waitHandle ** object is called, the WaitOne method that was called in the Main method returns and the application closes.

    static void workflowRuntime_WorkflowCompleted(object sender,
        WorkflowCompletedEventArgs e)
    {
        waitHandle.Set();
    }
    

To add the program source file to the project

  1. In the HostingWorkflows project file that you created in Task 1: Create the Workflow Hosting Project File, insert a new Compile node in the ItemGroup node that you created in Task 2: Create a Sequential Workflow.

  2. Add an Include attribute to the Compile node.

    Give this attribute the value "Program.cs" if you are using C#, or "Program.vb" if you are creating a Visual Basic project.

    <Compile Include="Program.vb" />
    
    <Compile Include="Program.cs" />
    

Compiling the Code

For information about compiling your code, see Compiling the Code.

In Exercise 2: Run a Workflow, you learn about the different ways to start and stop a workflow.

See Also

Reference

WorkflowRuntime
WorkflowInstance

Concepts

Creating a Workflow Host Application

Other Resources

Exercise 2: Run a Workflow

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