Friday, April 17, 2009

Creating Sequential Workflow Example with Windows Workflow Foundation (WWF)

This article describes how to create and run a Console Sequential Workflow in Windows Workflow Foundation with .NET Framework 3.0 and above. This shows the .net sample project to create sequential workflow step by step.

Pre-Requsities


  • .NET Framework 3.5 Framework
  • Visual Studio Team system 2008
    or
  • .NET Framework 3.0 Framework
  • Visual Studio 2005 Professional Edition
  • Windows Workflow Foundation extensions for Visual Studio 2005

Steps for creating a sample workflow project

1. Open Visual studio and Create a Sequential Workflow ConsoleApplication project as shown.


[WFImage1.JPG]

2. After creating the application, your designer window should look like this

[WFImage2.JPG]

3. From the ToolBox of the visual studio, add the code activity as shown in the below.

[WFImage3.JPG]

4. After code activity is added, Double click on the code activity. This creates codeActivity1_ExecuteCode method.

5. Add the code snippet as below

Initilize global variables


public sealed partial class Workflow1: SequentialWorkflowActivity
{
// Global Variables and used by activities
private int inputval = 0;
private string status = string.Empty;

}

Add the following code for code activity

private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
// Get the input from the user
Console.WriteLine("Enter a number");
// store the input in the global variable
inputval = int.Parse(Console.ReadLine().ToString());
Console.WriteLine("Workflow started....");
}

6. Go back to design view and click ifElseBranchActivity1 to add to the workflow. the properties window, you will see the Condition row. Click it and select Declarative Rule Condition. Now, expand the row and set Condition Name. Then add new Rule and then add the condition code for if loop like this.

Note: inputval is a global variable for the class

this.inputval <100
Do the same for else branch also with the condition
this.inputval > 100

7. After the condition is added for ifelseActivity , the designer will look like this

[Wfimage4.JPG]

9. Also add codeActivity3 and codeActivity4 in the if else loop to get the status. The final work flow diagram looks like this


[WFImage5.JPG]


10. Add the code for all the code activities
private void codeActivity2_ExecuteCode(object sender, EventArgs e)
{

// status is the global variable
Console.WriteLine(status);
Console.WriteLine("Workflow completed...");
Console.ReadLine();
}

private void codeActivity3_ExecuteCode(object sender, EventArgs e)
{
// set the global variable ... Also do custom code if any
status = "If Activity Executed....";
}

private void codeActivity4_ExecuteCode(object sender, EventArgs e)
{
// set the global variable ... Also do custom code if any
status = "Else Activity Executed....";
}

11. Now, press F5 or click Run in order to run the project. If the user input is 10, then the output will look like this

[WFImage6.JPG]

After completing the excerise, you have good understanding of creation of sequential workflow and it's use.