Chapter 41: Windows Workflow Foundation


This chapter provides an overview of Windows Workflow (known as WF throughout the rest of this chapter), which provides a model where you can define and execute processes using a set of building blocks called activities. WF provides a Designer that, by, default is hosted within Visual Studio, which allows you to drag and drop activities from the toolbox onto the design surface to create a workflow template.

This template can then be executed by creating a WorkflowInstance and then running that instance. The code that executes a workflow is known as the WorkflowRuntime, and this object can also host a number of services that the running workflows can access. At any time, there may be several workflow instances executing, and the runtime deals with scheduling these instances, and saving and restoring state; it can also record the behavior of each workflow instance as it executes.

A workflow is constructed from a number of activities, and these activities are executed by the runtime. An activity might send an e-mail, update a row in a database, or execute a transaction on a back-end system. There are a number of inbuilt activities that can be used for general-purpose work, and you can also create your own custom activities and plug these into the workflow as necessary.

This chapter will discuss the different types of workflow that are available out of the box, and describe some of the inbuilt activities that are available with WF. You’ll see how to use some of the standard activity types, and also create two custom activities to show how to extend WF. This chapter begins with the canonical example that everyone uses when faced with a new technology - Hello World - and also describes what you need to get workflows running on your development machine.

Hello World

To begin working with WF, you need to download both the Windows .NET Framework 3.0 and also the Visual Studio 2005 Extensions for Windows Workflow Foundation. The former does not need to be downloaded if you are running Windows Vista, as the Framework is included as standard in that operating system. Visual Studio 2005 Extensions for Windows Workflow Foundation is a setup package that adds workflow support to visual studio, and download details are available at the end of this chapter. Once this has been installed, you will see a new set of project types beneath the Workflow node within the New Project dialog, as shown in Figure 41-1.

image from book
Figure 41-1

Select Sequential Workflow Console Application from the available templates (that will create a console application that hosts the workflow runtime) and a default workflow that you can then drag and drop activities onto.

Next, drag a Code activity from the toolbox onto the design surface so that you have a workflow that looks like that shown in Figure 41-2.

image from book
Figure 41-2

The exclamation mark glyph on the top right of the activity indicates that a mandatory property of that activity has not been defined - in this case it is the ExecuteCode property, which indicates the method that will be called when the activity executes. You will learn how to mark your own properties as mandatory in the section on activity validation. If you double-click on the code activity, a method will be created for you in the code behind class, and here you can use Console.WriteLine to output the “Hello World” string as shown in the code snippet below.

  private void codeActivity1_ExecuteCode(object sender, EventArgs e) {     Console.WriteLine("Hello World"); } 

If you then build and run the program, you will see the output text on the console. When the program executes, an instance of the WorkflowRuntime is created, and then an instance of your workflow is constructed and executed. When the code activity executes, it calls the method defined and that outputs the string to the console. The section entitled The Workflow Runtime will describe in detail how to host the runtime. The code for the example above is available in the 01 HelloWorkflowWorld folder.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net