Task 3: Create a Workflow Host Console Application
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
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.
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;
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 { } }
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);
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) { }
Create a local variable named workflowRuntime of type WorkflowRuntime.
Start the Windows Workflow Foundation runtime engine by calling the StartRuntime method that was defined in the workflowRuntime object.
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);
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.
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.
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();
Call the WaitOne method that was defined in the waitHandle object.
This method does not return until the workflow finishes running.
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();
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.
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
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.
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
Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04