Windows Workflow Foundation is part of the .NET 3.0 framework. Windows Workflow (WF) allows the developer to create workflow enabled applications. This is the first part of the Introduction to Workflow series. In the first part I will discuss how to create simple Workflow application and custom activities.

Introduction:

Windows Workflow Foundation is part of the .NET 3.0 framework. Windows Workflow (WF) allows the developer to create workflow enabled applications. This is the first part of the Introduction to Workflow series. In the first part I will discuss how to create simple Workflow application and custom activities.

What is a Workflow?

The best way to describe a Workflow is by giving a simple example. Let’s say that you work in a company which sells some products. You get your products from the warehouse and it is really important that you notify the warehouse when the products are needed.

This whole process can be considered a workflow or combination of several workflows. The process of keeping track of selling items and notifying the warehouse of selling items is all part of a workflow application. Each of the task is performed in a desired manner. You will NOT order more products if you already have several of them in the storage.

Why Use a Workflow?

Why do we even require a Workflow? This all can be done using a simple application. You are right! This can be done by a simple application but there are number of advantages of using Workflow.

1)    The Workflow maintains the states of the progress. This means if the process is waiting for someone’s approval then it will wait at that state. This is performed using State Machines.

2)    Workflow applications are easy to understand. Workflow applications are created by using diagrams and can be understand easily by any person who is not in development field.

    3)    Workflow applications can contain custom entities which           are custom business components. This provides us with           the facility to quickly create a workflow application based           on the already developed components.

Downloading Windows Workflow Framework:

The first step is to download the Windows Workflow Framework. You can use the following link to download the framework:

Download Workflow Framework

Creating Windows Workflow Application:

Let’s start by creating a simple Windows Workflow Console Application. Open your Visual Studio.NET 2005 and choose C# and then workflow. From the project templates simply choose “Sequential Workflow Console Application”. Take a look at the screen shot below:

 


 

This will create a simple Workflow enabled console application.

Now, from the Toolbox drag and drop the Code item and place it between the start and stop marks as shown in the screenshot below.

 

 
The CodeActivity is used to execute custom code. The CodeActivity contains “ExecuteCode” method which is run when the workflow reaches the CodeActivity control. Let’s go ahead and give the world “Hello” by writing the message in our ExecuteCode method.

private void DisplayHelloWorld(object sender, EventArgs e)

        {

            Console.WriteLine("Hello World");

        }

That’s it! Now when you run the application you will see “Hello World” displayed on the console.

Congratulations! You have creating your first Windows Workflow Application. Now, let’s take a look at creating custom activities.

 
What are Activities?

Workflow is made up of several activities. You can consider activity as a particular task. In the last example we had the “CodeActivity” whose main purpose was to run the code inside the ExecuteCode method. There are several different types of activities available for workflow applications. Take a look in your Toolbox and you will find several pre-made activities.

(….after some time)


Good! So, you have looked at all the pre-made activities. Now, you might be thing why do you need custom activities when you already have several good pre-made activities? Let’s see why!

 
Creating Custom Activities:

Let’s say that another company was interested in your “Hello Worlflow” application want to use it in their own application. One way is to provide them with the necessary code so they can implement the same functionality. Another way and good way is to provide them with the “Custom Activity”. This way they can simply plug in your component with their workflow application.

In order to create “Custom Activities” you need to create a “Workflow Activities Library Project”.
 


 

By default it will have an activity class named “Activity1.cs”. Activity1 inherits from the SequenceActivity class. Let’s remove this inheritance and make it inherit from Activity class instead.

public partial class Activity1: Activity

Now, you must override the Execute method of the Activity1 class to execute your custom code.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

        {

            Console.WriteLine("Hello World");

            return base.Execute(executionContext);

        }

Congratulations! You have written your first Custom Activity.

 
Running Custom Activity:

Before you run the custom activity it is a good idea if you add it to the Toolbox. Simply, add the HelloWorldActivityLibrary.dll to the toolbox which in turn adds all the components available in that library. Once, the component is added in the toolbox simply drag and drop the activity on the workflow application.


 
That’s it! Now, when you run your workflow it will use the custom activity to display the “Hello World” message on the screen.

Conclusion:

In this article I demonstrated how to create a very simple workflow application. The article also covered creating a very simple custom activity.

I hope you enjoyed the article, happy programming!