Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Windows Workflow Foundation Essentials : Page 2

Applications that contain business processes and rules can benefit immediately from Windows Workflow's diagrams, class libraries, runtime, rules engine, and customization capabilities.

Sequential vs. State Machine Workflows
You can create two main types of workflows—sequential and state machine. The workflow shown in Figure 2 is a sequential workflow, which is similar to a flowchart or UML Activity diagram. The workflow executes activities in a prescribed order, defined in the workflow itself.

There are two types of diagrams to choose from—sequential and state machine. Sequential workflows are similar to flowchart or UML activity diagrams. State machine diagrams define a set of states with possible transitions between them.
In contrast, state machine workflows define a set of states with possible transitions between them. Events external to the workflow trigger transitions between states. This article covers only sequential workflows; a future article will discuss state machine workflows.

Ultimately, you can represent any sequential workflow as a state machine workflow and vice-versa. However, each type of workflow usually fits best with a particular application. Typically, if human interaction is involved as part of the workflow, a state machine is the best choice.

Creating a Workflow Host Application
Before creating a workflow you need to create a host application in which it will run. To do this, launch Visual Studio 2005, click the File menu, and select New → Project. In the New Project dialog box's Project Types pane under your preferred programming language, choose Workflow as shown in Figure 3.

Figure 3. New Project Templates: Workflow Extensions for Visual Studio 2005 adds project templates useful in creating Workflow-specific applications and libraries.

Figure 4. New Workflow: When you create a new Workflow host application it includes a Sequential Workflow diagram on which you can build.
In the Templates pane on the right, select Sequential Workflow Console Application to create a console application that will host the workflow. Change the name of the project to SequentialWorkflowConsole. After changing the project's location to a desired directory, click OK to create the workflow project. After your project is created, you'll see a new, empty sequential workflow, as shown in Figure 4.

As you can see by the diagram text that says "Drop Activities to Create a Sequential Workflow" the diagram is ready for you to add built-in activities or, as you'll see later, your own custom activities.

Go to the Visual Studio Toolbox and look at the Windows Workflow tab, which contains a predefined set of activities (see Figure 5). You can drag and drop these elements directly on your workflow diagram.

Figure 5. Toolbox Tab: The Windows Workflow Toolbox tab contains many useful, ready-to-use activities that provide basic functions and flow-of-control tools for your Workflow diagrams.
Next, go to the Solution Explorer and expand the project's References node to see the three System.Workflow assemblies that you have referenced in the project. As you might guess, these are references to the .NET 3.0 Windows Workflow assemblies.

In addition to diagrams, Windows Workflow includes a robust rules engine that allows you to define rules associated with your workflow.
To get a closer look at the actual workflow host program, open the Program.cs file (C#) or the Module.vb file (Visual Basic). As you can see in Listing 1 and Listing 2 these files contain the workflow host's Main() entry method.

Note the two lines that appear after the call to WaitHandle.WaitOne(). These lines display a console message, and then wait for console input. Adding these two lines to your workflow host program causes the console window to remain open after executing the workflow, allowing you to see its results.

Quick Start with the Code Activity
The easiest way to get a simple workflow up and running is to use the Code activity. I'll show you how to implement a simple workflow with a single Code activity so you can see how to use the workflow designer, workflow toolbox, and understand how the workflow host fits into the equation.

Figure 6. Error Icons: Activities can display error icons that indicate conditions that must be met for the activity.
Go to the Solution Explorer, and double-click the Workflow1 diagram to display it in the designer. From the Visual Studio Toolbox, drag a Code activity and drop it on the sequential workflow diagram as shown in Figure 6. You'll see a red exclamation mark error icon located on the upper-right corner of the Code activity. If you click the icon's smart tag, a drop-down displays the error: Property "ExecuteCode" is not set. The error indicates that you must set the Code activity's ExecuteCode property to make the activity functional. Errors such as this are a great feature of the Windows Workflow designer that lets activity authors specify the steps an activity user must take to make the activity functional (usually setting a property or creating an event handler method).

To satisfy this condition of the Code activity and eliminate the error, double-click the activity in the diagram to create an event handler for the activity's ExecuteCode event. Then, add the following code to the handler—in this case, a simple write operation to the console:

// C# private void codeActivity1_ExecuteCode(object sender, EventArgs e) { Console.WriteLine("Hello WF World!"); } ' Visual Basic Private Sub codeActivity1_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs) Console.WriteLine("Hello WF World!") End Sub

Now that you have created a workflow host application and a simple workflow with one activity, you are ready to run it.

Running Your First Workflow
I highly recommend setting a breakpoint at the top of the host application's Main method so you can step through the instantiation of the workflow runtime and the workflow itself.

In C# open Program.cs, or in Visual Basic open Module.vb, and set a breakpoint on the using statement in the Main() method.

Press F5 to run the workflow host program, and then press F10 to step through each line of the workflow host Main() method. As you step through the code, note these key actions that occur in the method:

Figure 7. Completed Workflow: The hosting console application displays the "Hello WF World" message created by the Code activity.
  • First, the WF runtime gets instantiated. This must happen first because WF uses the runtime to instantiate, execute, and manage your workflows.
  • Handler methods get registered for the workflow runtime's WorkflowCompleted and WorkflowTerminated events. This allows the workflow host program to be notified whenever a workflow completes normally or prematurely terminates.
  • The workflow gets instantiated. Notice that the host program does not instantiate the workflow directly, but by calling the workflow runtime's CreateWorkflow() method.
  • Finally, the method calls the workflow's Start() method, which begins executing the workflow.
When the workflow is complete, you will see a console window displaying the text "Hello WF World!" as shown in Figure 7. Press any key to close the console window, and then press F5 to exit the workflow host program.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date