Windows Workflow Foundation (WF) is a component of .NET framework 3.0 and 3.5
and is part of Windows platform for developers. To understand this component
let's first get introduced to a workflow. Business process consists of number of
activities related to app development, and specifying these activities in a
series of steps is known as a workflow. WF provides a common foundation for
building workflow-based app on Windows.
Design schemes in app development are always separated from actual codes to
be implemented, making design schemes mere documentation after app is developed.
To overcome this separation, Microsoft came out with Windows WF, which can be
used in document mgmt, business cycles, or page flow.WF is easy to understand as
it gives a visual representation of process and extending existing workflow is
much simpler in it.
Direct Hit! |
Applies To: .NET Developers USP: Integrating design and app Primary Link: http://msdn.microsoft.com/workflow/ Keywords:Windows Workflow Foundation |
Components of WF
WF consists of number of components-activity that is a unit of work;workflow,
a collection of activities; workflow designer, used to create graphical
representation of workflow; base activity library, a collection of activities we
can use in our workflow; runtime engine, to execute workflow; runtime services
to support workflow execution; and host process which is a Windows app that
hosts WF runtime engine and workflow it executes.
Workflow types
WF was built to support activities in a business process, now in a business
process activities can be categorized into two: system related and human
related. WF tries to support both these activities with following types of
workflow:
Sequential workflows: used in apps where activities are executed in a defined
order and path of execution is known, ie system activities.
State-machine workflows: used in apps where path of execution of activities
is not predefined. Human workflow can be implemented using this type of
workflow.
Implementation
In this article,we'll implement a sample sequential workflow using .NET
framework 3.5 and VS 2008. This will explain how sequential workflow actually
works.
We will start with creating a new project in VS 2008, in project dialog window
expand Visual c#/workflow; the project type here would be sequential workflow
console app, this step will bring you in workflow designer page. Use toolbox to
get various types of activities, here we are using code activity, just drag and
drop code activity on the designer page. Code activity will show a red
exclamation mark signifying that it should be configured. Simplest way of
configuring it is to double click on code activity and add display code into it
as shown in this code snippet:
In our implementation we used VS 2008 and from base activity we chose code activity and ifElse activity |
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System.Workflow.Activities;
namespace WorkFlowExample
{public partial class Workflow1 : SequentialWorkflowActivity
{private void codeActivity1_CodeHandler(object sender, EventArgs e)
{Console.WriteLine("Intro to WF");
Move to design page and add breakpoint by right clicking on code activity
box. Save and start debugging this project. You will see how control moves
through code activity and displays message. Now let us build a new project
similar to one we just created but this time use IfElse activity to get two
paths for our workflow, and depending on parameters mentioned workflow should
give desired output. In a new workflow console project drag and drop IfElse
activity and then add two code activities in designer view.
There would be exclamation mark on ifElceBranhAtivity1, click on it and move
to properties, from the drop down box in condition property select declarative
rule condition. Expand condition property and click on button that is there in
front of ConditionName property. Add condition as shown in figure 2, repeat
similar steps for ifElceBranhAtivity2.
Add following code snippet in code view of workflow:
Click on new and add code in the new Window . Here we are adding simple condition, ie, this.prise=100 for ifElceBranhAtivity1 and this.prise!=100 for ifElceBranhAtivity2 |
string name;
int prise;
string message;
public string Name
{get { return name; }
set { name = value; }}
public int Prise
{get { return prise; }
set { prise = value; }}
public string Message
{get { return message; }}
private void codeActivity1_ExecuteCode_1(object sender, EventArgs e)
{ifElseBranchActivity1:
message = string.Format("the prise of {0} is accepted", name);}
private void codeActivity2_ExecuteCode_1(object sender, EventArgs e)
{ifElseBranchActivity2:
message = string.Format("the prise of {0} is rejected", name);}
Move to Program.cs file and replace last three lines with the following code:
int a;
string b;
Console.WriteLine("Enter product name");
b = Console.ReadLine();
Console.WriteLine("Enter prise");
a = Convert.ToInt16(Console.ReadLine());
Dictionary
parameters.Add("Name", b);
parameters.Add("Prise", a);
WorkflowInstance instance = workflowRuntime.CreateWorkflow(
typeof(WorkflowConsoleApplication6.Workflow1), parameters);
instance.Start();
waitHandle.WaitOne();
This is done to pass parameters to workflow and also get parameters from it.
We also need to edit method in WorkflowCompleted event to:
workflowRuntime.WorkflowCompleted += delegate(object
sender, WorkflowCompletedEventArgs e)
{Console.WriteLine(e.OutputParameters<"Message">);
Console.ReadKey();
waitHandle.Set();};
Now go back to design view of workflow and add break points to Codeactivity1
and 2. Start debugging this workflow, you will see how control moves through the
workflow design and selects path based on parameter passed in host app.
The above two programs show how code and design are integrated in a single
app. Also, one can choose from no. of other activities and build custom
activities.