Windows Workflow Foundation Series
Windows Workflow Foundation – Part 1 This
article will focus on at the basics of Windows Workflow Foundation by discussing its architecture, key building
blocks that make up Windows Workflow Foundation. This article will also provide a simple example that shows how to
create a simple workflow using Visual Studio 2005.
Windows Workflow Foundation – Part 2 This
article will discuss the integration between workflow and Web services through examples. Specifically this article
will provide a simple example that shows how to invoke a Web service from within a workflow. Finally, you will
also see the steps involved in exposing the workflow as a Web service by leveraging the integration between Visual
Studio 2005 and Windows Workflow.
Windows Workflow Foundation – Part 3 This
article will focus on those built-in activities that provide you with control over your workflow conditional
logic. Specifically this installment will discuss Windows Workflow control flow activities such as IfElse, While,
Parallel, Sequence, and Replicator activities by discussing the usage of these activities through examples.
Prerequisites
To follow along with the examples throughout this article series, you need the following installed on your
system:
- .NET Framework 3.0 - Because Windows Workflow Foundation is built on top of this, you need
the .NET Framework 3.0 installed for runtime functionality. Windows Vista comes with the .NET 3.0 Framework out of
the box as long as you select this option at the time of installing Windows Vista. If you are running an operating
system other than Windows Vista, you need to install the .NET Framework 3.0.
- Visual Studio 2005 (any edition) - Required for
workflow development.
- Windows Workflow Foundation extensions for Visual Studio 2005 - Available for download from Microsoft.
Activities in Windows Workflow
As mentioned previously, Workflows are made up of a set of activities. An activity can be defined as the
smallest piece of action that the flow employs. An activity can be anything from a straightforward action to a
composite action that consists of an assembly of several activities. They can be aggregated to a workflow by using
the workflow visual designer supplied with Visual Studio 2005. Activities can also be sequential, as the whole
workflow itself, where the order of the steps is predetermined at design time, or it can be event-driven, where
the order of the actions is determined at run time in response to external events, as in the case of state machine
workflow.
For the purposes of demonstrating activities, let us create a new Visual Studio project by selecting File ->
New from the menu. In the New Project dialog box, select Workflow under the Visual C# node in the Project types
pane and select Sequential Workflow Console Application as the template in the right pane. Name the project
WorkflowActivities as shown below:
Figure 1: Creating the WorkflowActivities project

After the project is created, select View -> Solution Explorer from the menu so that
you can examine the files within the project. The Workflow1.cs file is the workflow itself, and the
Program.cs file contains the .NET code that starts the Console application. To start with, add a new
workflow named IfElseWorkflow to the project and modify the below line of code in the
Program.cs file to invoke that workflow.
Every time you add a new workflow to the project, just modify the above line of code in the
Program.cs file to create the instance of the new workflow for invocation purposes.
IfElse Activity
Using the IfElse activity, you can simulate an if-then-else conditional expression. As you would
imagine, the IfElse activity requires you to provide the logic that evaluates to a Boolean value.
Windows Workflow enables you to provide this logic using two different ways.
One method of providing this logic is to set the Condition property of an
IfElseBranch activity to an instance of the DeclarativeRuleCondition class. This class acts as a
pointer to a rule that exists in the workflow’s rules set.
Another way of providing this logic is to set the Condition property to an instance of the
CodeCondition class. This class exposes an event, also called Condition that is fired when the
IfElseBranch activity’s logic needs to be evaluated. The event handler accepts an argument of type
ConditionalEventArgs (that is of type Boolean) through which you can specify the results of the conditional
expression you build into the event handler. You accomplish this by setting the Result property of
the ConditionalEventArgs object to either true or false. Depending on the
Result value, the IfElse activity directs workflow execution to one of two branches.
Visually, in the Microsoft Visual Studio workflow visual designer, true executes the path shown on
the left and false executes the path to the right. Note that both of these branches are containers
for other activities meaning that you can insert child workflow activities that encapsulate the logic of the
workflow depending on the result of the condition evaluation. The below example leverages this approach to provide
the logic to the IfElse activity.
For the purposes of this example, let us pass a parameter to the IfElseWorkflow and use its value
to drive the IfElse condition. To this end, add a new private variable named
_approvalRequired to the workflow and corresponding getter and setter methods.
From within the workflow, let us examine the value contained in the ApprovalRequired
parameter in the IfElse activity and execute appropriate logic inside the If and Else condition.
Now drag and drop an IfElse activity from the tool box onto the workflow designer between the
Start and End steps. After the activity is added, you will see a red icon indicating that the condition property
has not been set. Click the down arrow right next to the icon to set the condition property. That will bring up
the properties dialog box through which you can set the Condition property to either CodeCondition or
DeclarativeRuleCondition. For the purposes of this example, select the CodeCondition from the list box and set its
Condition subproperty value to IfElseCondition_ExecuteCode and click Enter to bring up
the code window.
Drag and drop a new Code activity right below the left
IfElseBranchActivity and set its ExecuteCode property to
IfCondition_ExecuteCode. Now modify the IfCondition_ExecuteCode procedure to look as
follows:
Now add another Code activity right below the right IfElseBranchActivity
and set its ExecuteCode property to ElseCondition_ExecuteCode. Now modify the
ElseCondition_ExecuteCode procedure to look as follows:
Now modify the workflow invocation code in the Program.cs file to assign a value to the
ApprovalRequired parameter in the workflow.
Now you are ready to execute the workflow through the console application by pressing F5. You should see an
output that is somewhat similar to the following.
Figure 2: Output for the IfElse activity example

Since you passed in false to the ApprovalRequired parameter,
the above picture shows that being reflected inside the workflow.
While Activity
The While activity is very similar to the while loop construct in programming
languages such as C#. Like the IfElseBranch activity, which uses its Condition property
to determine whether the branch executes, the While activity also has a Condition
property. You can set the Condition property to either Rule based Condition or Code Condition,
similar to the IfElse activity.
Note that the While activity can contain only one child activity. You can overcome this limitation
by using the Sequence activity as the root child activity of While activity and add
child activities to the Sequence activity.
For the purposes of this example, add a new workflow named WhileWorkflow to the
WorkflowActivities project. For the purposes of this example, let us consider a simple scenario
wherein you want to execute the logic contained inside the While activity specific number of times
that is decided at run time. To this end, let us start by declaring a public property inside the workflow that
specifies the number of times the logic inside the while loop needs to be executed.
In addition to the _count variable, let us also add another private variable named
_noOfTimesExecuted that keeps track of the number of times the While logic is
executed.
Now that you have added the
required private variables, drag and drop a While activity to the workflow designer and set its
Condition property to Code Condition. Set the value of the Condition sub property to
WhileActivity_CodeCondition and click Enter to bring up the code window. Modify its event handler to
look as follows:
Now drag and drop the Code activity as the child of the While activity and
modify its ExecuteCode event handler to look as follows:
Now that you have created the workflow, the next step is to modify the Program.cs
file to invoke the WhileWorkflow. If you pass in a value of 5 to the Count
parameter, you will see an output that is somewhat similar to the following.
Figure 3: Output for the While activity example

Parallel Activity
As the name suggests, the Parallel activity enables you to have multiple activities executing
independent of one another. Once you configure the Parallel activity with multiple branches, all of
those branches execute in a semi-parallel fashion at run time. Note that each branch requires a
Sequence activity as its first child. Although this activity is named Parallel, the
branches don’t execute in a true parallel nature. This is due to the fact that workflows are executed on a single
thread. Therefore, multiple branches cannot run at the same time. Because of this limitation, the Windows Workflow
simulates parallel execution. For example, the Parallel activity runs the first activity of the first
branch, and after that activity’s execution has completed, the first activity of the second branch is run. This
process continues until all activities in all branches have been executed. The following picture shows the result
of placing a Parallel activity onto the workflow designer.
Figure 4: A Parallel activity in the Workflow Designer

Although you see only two branches as children of the Parallel activity,
you can add additional branches through the "Add Branch" hyperlink that can be found at the bottom of the
properties dialog box.
Sequence Activity
Sequence activity is a utility activity that is fundamental to the creation of sophisticated
workflows. As the name suggests, it is a container activity that allows you to host multiple child activities and
subsequently have them execute in a defined order. The Sequence activity is mainly used in
conjunction with other activities that allow only one child. For example, you can use the While
activity with a Sequence activity to allow multiple children to execute in a loop iteration. In
addition, the branches of the Parallel activity require that their sole child be a
Sequence activity.
Replicator Activity
This activity creates any number of instances of its single child activity. You can combine this activity with
the Sequence activity or any other composite activity to allow multiple children. The
Replicator activity has a collection property called CurrentChildData that dictates how
many times the child activity is replicated. You can populate this collection in an event handler known as
Initialized. The below table provides a brief overview of the events exposed by the
Replicator activity.
| Event | Description |
ChildCompleted | This event is raised when the child activity of the
Replicator activity has completed its execution |
ChildInitialized | This event is raised when the child activity of the
Replicator activity has initialized |
Completed | This event is raised when the Replicator activity has
completed its execution. Note that this event is raised only once for the Replicator. |
Initialized | This event is fired when the Replicator activity begins
its execution. Unlike the ChildInitialized event, this event is fired only once prior to any child activity
execution |
UntilCondition | This event is raised prior to the execution of each
child activity instance. Through the ConditionalEventArgs argument supplied to its event handler, you can control
the continued execution of the loop |
In addition to the rich set of events, the Replicator activity has a property called
ExecutionType, which you can set to one of two values in the ExecutionType enumeration.
If you set this property to ExecutionType.Sequential, an instance of the child activity is created and executed
before any other instances are created and subsequently executed.
If you set the ExecutionType property to ExecutionType.Parallel, all child activity
instances are created first and then executed in a parallel fashion.
In some situations, you might want the execution of child activities to be canceled if a certain condition
evaluates to true. In those cases, you can set the UntilCondition property to the
desired code or rule condition.
Similar to a Foreach statement, the Replicator activity creates and executes a given
number of instances of the specified child activity. You can specify only one child activity, but using composite
or custom activities is allowed. You can't control the number of iterations through a declarative property.
Instead, you write a handler for the Initialized event and populate the CurrentChildData
collection with initialization data for each of the desired instances:
The above code snippet adds two instances of the child activity to the replicator, each initialized
with a given string. Note that if you leave the CurrentChildData collection empty, the
Replicator won't run any child activity and is limited to firing top-level events such as
Initialized and Completed. Now drag and drop a Code activity inside the
Replicator activity and modify its ExecuteCode event handler to look as follows:
By default, child instances run in sequence. However through the ExecutionType
property, you can opt for a parallel execution. If no global condition is set through the
UntilCondition property, the replicator ends when all child activities have completed execution. If
the UntilCondition property is set, the activity terminates when the UntilCondition is
true.
Conclusion
Activities are the reusable building blocks for composing workflows. In this installment, you have understood
the steps involved in leveraging out of the box activities to create your workflows. Specifically you learned how
to embed flow logic in your workflow by looking at the activities that are specifically meant for representing
conditional flow logic. To this end, you explored out of the box activities such as IfElse,
While, Replicator, Parallel and Sequence activities. The next
installment in this series will get into the details of creating custom activities through Windows Workflow.
About Thiru Thangarathinam
 |
Sorry, no bio is available
This author has published 4 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
WPF 4 (VS 2010 and .NET 4.0 Series)
read more
My History of Visual Studio (Part 6)
read more
My History of Visual Studio (Part 2)
read more
My History of Visual Studio (Part 3)
read more
SOA Patterns presentation on E-VAN (recording)
read more
GiveCamps Get a new Sponsor
read more
Announcing the WebsiteSpark Program
read more
Scenarios for WS-Passive and OpenID
read more
More On The CodePlex Foundation
read more
Foxit PDF Previewer update
read more
|
|
Please login to rate or to leave a comment.