Introduction to Windows Workflow 4.0 And Visual Studio.NET 2010


Introduction

With the new release of Visual Studio.NET and Microsoft.NET framework, new features are added to the underlying technologies.

In this article, I want to illustrate the programming model, engine and tools to build workflow-based applications using Windows Workflow 4.0. As you know workflows are a way to execute business processes and in today's applications supporting business processes we are frequently using Windows Workflow technologies as the key technical solution. The example in this article is the traditional Hello World application in order to illustrate the creation, hosting and running of a workflow. Then we're going to extend the business process with a conditional activity to print a different message depending on the context and finally I will show how to handle exceptions in Windows Workflow 4.0.

Getting started with the solution

First of all, let's open the Visual Studio.NET 2010 and create a new Workflow Console Application (see Figure 1).

1.gif

Figure 1 

Then the Workflow1.xaml file is open on the Windows Workflow Designer. Since our business process is just one activity (print a message on the console), let's drag and drop a WriteLine activity from the Toolbox onto the workflow designer. Then select the Text property and enter the string "Hello World" string (see Figure 2).

2.gif
 
Figure 2

In order to run this workflow, we need a host engine. The easiest way is to create an instance of the WorkflowInvoker class passing a reference to an instance of the workflow (see Listing 1).

using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;

namespace HelloWorldWorkflowConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            WorkflowInvoker.Invoke(new Workflow1());
        }
    }
}

Listing 1

After you run the application, you will receive the text on the console (see Figure 3).

3.gif
 
Figure 3

Another way to invoke a workflow is with the WorkflowApplication class. This class allows you to run a workflow on a separate thread and to supply delegates that will be invoked when the workflow completes, goes idle, terminates or has an unhandled exception. This allows you to create multi-threaded server or client programs more easily than you can without workflows.

Now let's add some complex logic to the workflow. Let's suppose our business process dictates that if we pass a parameter with a username, then the greeting is customized, otherwise the greeting is generic.

Delete the WriteLine activity and drag and drop a Sequence activity from the Toolbox onto the designer. In order to pass data into and out of the workflow, we use Arguments (you can see in the bottom of the workflow designer). Variables represent the storage of data. Now click on the variables button (while selecting the Sequence activity) and add a strGreeting variable, setting as the default value the string "Hello World" (see Figure 4).

4.gif
 
Figure 4

Then, let's add the strUsername argument (see Figure 5).

5.gif
 
Figure 5

The next step is to test the value of the strUsername argument. Drag and drop the IF activity from the toolbox inside the Sequence activity in the workflow designer. Then click on the Condition to set the logical condition (see Figure 6).

6.gif
 
Figure 6

Expressions are program statements that can be a literal string, a conditional statement, or an expression that concatenates several strings or calls a method or invokes another activity. Expressions are written using Visual Basic syntax even if the application is in C#. This means capitalization does not matter, comparison is performed using a single equals sign instead of "==", and the Boolean operators are the words "And" and "Or" instead of the symbols "&&" and "||".

Then drag an Assign activity to the Then side of the conditional control. We're going to assign to the strGreeting variable a value due to the value of strUsername variable. Click on the To property of the Assign activity and set a reference to the strGreeting variable (see Figure 7).

7.gif
 
Figure 7

Click on the Value property of the strGreeting variable and set the following value (see Figure 8).

8.gif
 
Figure 8

And finally, let's add a WriteLine activity. Click on the Text property and set a reference to the strGreeting variable (see Figure 9).

9.gif
 
Figure 9

The final workflow resembles as the Figure 10.

10.gif
 
Figure 10

Next step is to go to the code to invoke the workflow and make the changes necessary (see Listing 2).

using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;

namespace HelloWorldWorkflowConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Workflow1 objWorkflow = new Workflow1();
            objWorkflow.strUsername = "John Charles Olamendy";
            WorkflowInvoker.Invoke(objWorkflow);
        }
    }
}

Listing 2

And running the application, we have (see Figure 11).

11.gif
 
Figure 11

Notice that if you pass no value to the workflow argument, then you receive an exception. To work correctly the application, you should pass a blank string ("").
In the last part of the article, we're going to add error handling capabilities to the workflow using the Try/Catch approach of C# language.

Now let's drag and drop a TryCatch activity onto the workflow. In order to move the If and WriteLine activities inside the TryCatch activity, add a Sequence activity inside the Try section and drag and drop the If and WriteLine activities inside this sequence. And finally, click on the Catches section and select the System.NullReferenceException option.

The final workflow resembles as shown in the Figure 12.

12.gif
 
Figure 12

Conclusion

In this article, I've shown how to create, host and run a workflow using the new features of Windows Workflow 4.0 such as tools, designers and engine. Now you can build your own workflow solution.


Similar Articles