Workflow Foundation part of the Microsoft .Net 3.0 framework is a wonderful new framework that allows you to design and create asynchronous processes using visual studio. Before we start with the framework itself we need to understand why do we have this framework in the first place? The answer is simple, it serves a common need. In business application it is common to capture the business processes and allow the user to run through it using a set of screens. A common example could be a user trying to get a day's leave or vacations.
The process would start out with the user going to a screen and launching the request, this would then be seen by HR who would redirect it to the manager of that user, it is then approved or disapproved and the result returned to HR then to the user. Building such processes was a common pattern in business applications, so there was a need for a framework to generalize this task and allow for easy creation of such processes. WF as a framework differs from the packaged BPM solutions like K2.Net, which offer a complete set of tools for doing just this one task and is typically directed towards implementers. WF on the other hand is for developer to create custom implementations for whatever need there may be.
To start development with WF you need to have WF Extensions for VS 2005 and .Net Framework 3.0, if you are using Visual Studio 2005. If you have Visual Studio 2008 then you are already setup for development. Workflow Foundation is not that simple a framework to understand and required certain level or proficiency and maturity in both .net and development in general.
Let's start by writing some workflow code from scratch. If you start by creating a console application in C#, you need to add the following three references:
- Microsoft.Workflow.Activities
- Microsoft.Workflow.ComponentModel
-Microsoft.Workflow. Runtime
Start by adding the following:
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
Define a class something like this:
class MinimalWorkflow : SequentialWorkflowActivity
{
public MinimalWorkflow()
{
CodeActivity ca = new CodeActivity();
ca.ExecuteCode += delegate
{
Console.WriteLine("Hello World");
};
this.CanModifyActivities = true; //this is not required in VS2005
this.Activities.Add(ca);
this.CanModifyActivities = false; //this is not required in VS2005
}
}
The class above derives from the SequentialWorkflowActivity because everything in WF is considered an activity. Activity is the basic unit of workflow and the whole workflow itself is also an activity. There are two types of these activities, basic and composite. So you can guess that a basic activity would perform a task while composite would be anything that is made of other activities. Workflow itself is a composite activity and in this case it is sequential (step by step) in nature. CodeActivity is one of the basic activities that we can add to the workflow. All actions in the activities are event driven hence we put a delegate on the execute event for this activity.
The program that hosts or loads a workflow in memory is called the 'host' application. In our example we might write one like follows:
class Program {
public static void Main(string[] args)
{
using (WorkflowRuntime runtime = new WorkflowRuntime())
{
WorkflowInstance instance = runtime.CreateWorkflow(typeof(MinimalWorkflow));
instance.Start();
Console.ReadLine();
}
}
}
At the minimum one needs to create the workflow runtime and using it instantiate the workflow. I am using an overload of the CreateWorkflow function that requires only a type, there are other that allow you to pass parameters and setup other details as well. After creating the workflow we need to start it. Since this is an asynchronous application the main function might exit without giving the workflow a chance to complete, hence the Readline. It ensures that the workflow will get enough time to load and trigger all its events.
This concludes part one. In succeeding posts I will cover different aspects of WF and explain relevant details with that aspect.