Workflow Foundation (WF) library is one of the Windows Vista suites of libraries and ships as a part of .NET Framework 3.0 SDK, formerly known as WinFX SDK. It allows developers to create Workflow based applications. This article is a basic introduction of Workflow Foundation library and shows how to write your first WWF application using Visual Studio 2005.
What is a Workflow?
Workflow based applications have been around for a while and usually being used in Enterprise development. A workflow is a set of activities represented in declarative languages such as XML. Each activity in a workflow is a set of actions to be taken in respect to some business logic. From a programmer's perspective, an activity is some piece of code that executes some business logic. For example, an IF .. ELSE statement can be an activity.
WWF provides common activities used in applications but you can write your own custom activities if needed.
What is Workflow Foundation (WF)?
In .NET Framework 3.0, the API that is used to create workflow based applications is called Workflow Foundation (WF). The WF provides you a choice to write your workflow solely in XAML (see XAML section on Longhorn Corner to learn more about XAML) or a combination of XAML and a .NET programming language such as C# or Visual Basic. If you write a workflow in an XAML file only, there is no need to compile the code. All functions will be loaded dynamically at run-time. However, if you use a combination of a code-behind file and XAML, then you will have to compile the code.
Prerequisites
I am going to use Visual Studio 2005 to develop Workflow based applications. To write Workflow applications, you must install .NET Framework 3.0 SDK (see downloads section of MSDN) after installing Visual Studio 2005. Workflow Foundation is supported on Windows Vista, Windows XP SP2, and Windows Server 2003 operating systems.
To download and getting started with WWF, read Basic Introduction to Workflow Foundation by Dipal Choksi.
Creating Workflow Based Applications using Visual Studio 2005
To understand the internals of a workflow and workflow based application, let's create an application using Visual Studio 2005 and do some reverse engineering.
Visual Studio 2005 supports workflow based applications. To create a workflow based application, select New Project, Visual C# and select Workflow option from the list. You should see various workflow based templates in the Templates section. See Figure 1.

Figure 1. Selecting a Workflow based template in Visual Studio 2005
Figure 1 shows there are 6 templates to create different workflow applications in Visual Studio 2005 listed as following:
- Sequential Workflow Console Application
- Workflow Activity Library
- State Machine Workflow Library
- Sequential Workflow Library
- State Machine Console Application
- Empty Workflow Project
First option allows us to create a sequential workflow application. Selecting Workflow Activity Library, State Machine Workflow and Sequential Workflow Library options allows us to create activity, state machine and workflow libraries. You may use State Machine Console Application option to create a state machine application and the last option creates an empty workflow project where you will have to start by scratch. I shall discuss all of these projects types and their details in my forthcoming articles.
Selecting OK button in Figure 1 creates a workflow application.
Exposing Visual Studio Code
Creating a workflow is just a matter of generating correct XML. You may create a workflow by hand constructing the XML. Alternatively, you may want to use Visual Studio 2005 designer, which provides drag and drop feature. To create a workflow, you simply need to drop the activities from the Toolbox to the designer and code will be written for you.
After you create a Workflow application (Figure 1), Visual Studio generates the workflow code for you and the default page looks like Figure 2.
Figure 2 Default sequential workflow
The default workflow in Figure 2 has two activities – Begin and Terminate. Now to create the complete workflow, you simply have to add activities between the Begin and Terminate activities.
Understanding Workflow Runtime
Before we put our application to work, let's take a look at the project files in the Solution Explorer. You will notice a file Program.cs. See Figure 3.

Figure 3. Solution Explorer view of workflow application
Figure 5 shows the contents of Program..cs. As you can see from this code, WorkflowRuntime is responsible for creating a Workflow runtime object and StartWorkflow method starts the workflow, which takes the Workflow object as a parameter and StopRumtime method stops the workflow runtime.

Figure 4. Code-behind of Program.cs
As you can see from workflow.Runtime.StartWorkflow(type) line of code, it is actually executing our workflow class which is of type Workflow1. I will discuss WorkflowRuntime and other classes in my forthcoming in-depth articles.
So a Workflow based application has two parts - The Workflow class and the caller class. As you see from the above discussion, I had a Workflow.cs class that contains all the activities and related code. The second class involved in the application was Program.cs, which is a caller application, where we had WorkflowRuntime executes the Workflow class. So keep this in mind, we can build our workflows independently of the caller application and any application then call these workflows. This theory will play a vital role when I will be implementing real-world large scale workflow-based components and applications.
Putting Workflow to Work
Now let's have our workflow do something for us. I drop a Code activity from the Toolbox to the designer as you can see in Figure 5.

Figure 5. Designer with Code activity
By dropping a Code activity to the designer, the designer adds following definition of Code activity to Workflow.Designer.cs class:
private Code code1;
The designer also adds the following code to the InitializeComponent method, which creates an instance of the System.Workflow.Activities.Code class and sets its ID. In the end, adds the code1 activity to the activities of the workflow.
this.code1 = new System.Workflow.Activities.Code();
//
// code1
//
this.code1.ID = "code1";
this.Activities.Add(this.code1);
Now if you double click on code1 activity in the designer, the designer adds the following line of code to the designer class in InitializeComponent method.
this.code1.ExecuteCode += new System.EventHandler(this.code1_ExecuteCode);
The code1.ExecuteCode event handler will be called when the activity is called for the execution.
The designer also adds the following code to the Workflow.cs class. Now whatever code you will write in this event handler, it will be executed when the workflow activity will be called.
private void code1_ExecuteCode(object sender, EventArgs e)
{
}
Let me add the following code to this event handler.
private void code1_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("Test");
Console.Read();
}
And run the application. The output looks like Figure 6.

Figure 6. Output of workflow application
Now using the same process, you can add as many sequential activities to the workflow and they will be executed in the sequence.
Summary
This article was an introduction of Workflow Foundation library available in Windows Vista. In this article, we learned how to write our first simple workflow based application in Widows Vista using Visual Studio 2005. In my forthcoming articles, I will cover other workflow types and more internals of related activities. Stay tuned.