Click here to Skip to main content
15,610,588 members
Articles / Web Development / ASP.NET
Posted 21 Sep 2008


188 bookmarked

Windows Workflow Foundation FAQ

Rate me:
Please Sign up or sign in to vote.
4.59/5 (64 votes)
5 Jul 2009CPOL9 min read
A Windows Workflow Foundation FAQ

Table of contents


In this FAQ, we will quickly run through and get a feel of how WWF (Windows Workflow Foundation) will help you in making custom workflows in your project.

If you have not read my previous quick FAQ section, you can always read from below:

Happy job hunting......

What is Windows Workflow Foundation?

WWF is a programming model for building workflow-enabled applications on Windows. The System.Workflow namespace has all the necessary modules to develop any type of workflow.

What is a workflow?

A workflow is a set of activities which is stored as a model and they depict a process. The figure below clearly depicts the difference between a Workflow and an Activity. Every task is an activity and a group of activities depicts a complete workflow. A workflow is run by the workflow runtime engine.

Image 1

Figure 1: Workflow Foundation Architecture

The workflow model can be written in pure .NET code, pure XAML, or a mix of XAML and .NET code. A workflow model is compiled and can execute under Windows, ASP.NET, Web Services, or Windows Services application.

What are the different types of workflows in Windows Workflow Foundation?

There are two basic types of workflows: sequential workflow and state machine workflow. A sequential workflow has clear start and finish boundaries. The workflow controls the execution in sequential workflow. In sequential execution, one task is executed after another. A sequential workflow is more rigid in format and the execution path has a deterministic nature. A state machine workflow is more dynamic in nature. The workflow has states and the states wait for events to help them move to the next state. In state machine, execution path is indeterministic in nature. The figure below shows the visual conceptualization of the fundamentals. You can see in the sequential workflow that the execution path is very deterministic. Shiv performs the entire tasks sequentially and these tasks are very deterministic. Now have a look at the second workflow. Every state goes to another state when it receives some external events. For instance, when Shiv is seeing Star Trek, there is an event of flashing news which triggers him to see the flashing news.

Image 2

Figure 2: Sequential and state machine workflow

When should we use a sequential workflow and when should we use state machines?

If the workflow is very rigid, then you go for sequential workflow, and if the workflow is dynamic, then go for state machine workflow. For instance, you have placed an order and the order will not pass until your supervisor approves it is a rigid flow. Because your order has to be approved by a supervisor, or else it will not be approved. But what if your order moves from one place to another? For instance, it moves from approval to waiting, then clarification of the state machine workflow model is more appropriate. Below is a simple code snippet which shows practically how to use a sequential workflow. Let us try to understand step by step, what is marked in the figure:

  • 1 - First you need to select the System.Workflow namespace.
  • 2, 3, and 4 - In these three steps, we create the code objects and link them with activities.
  • 5, 6, 7, and 8 - We start the workflow and create a workflow instance object to run the sequential workflow. You can see the output in 8. Depending on how you add the activity in section 3, it executes sequentially. Because we have added codeactivity1, first it executes the first activity first. The sequence on how you add the activity to the activities collection is how the activities are run.

Image 3

Figure: 3: Code Snippet for Workflow

Note: The above code snippet was developed without using a designer. The whole point was to make you understand what happens behind the scenes. In real projects, you will be dependent on a designer rather than coding manually. You can find the above code in the SimpleWorkFlowSampleManual folder.

How do we create workflows using a designer?

As said previously, it is very easy to design workflows using a designer. We will answer this question by actually doing a small sample. Below is a code snippet and image snapshot which shows how we can use a designer to create workflows. Let's understand the snapshot below.

  1. First select a sequential workflow project. In this case, we have selected a sequential workflow console application to keep the sample simple.
  2. When you are done with creating the project, you will see the Solution Explorer as shown in the second snapshot. There are two files: WorkFlow1.cs and Workflow1.designer.cs. If you click on WorkFlow1.cs, you will get a designer pane as shown in snapshot 3. If you double click on Workflow1.designer.cs, you will get behind the code as shown in snapshot 4.
  3. Let us drag drop a code activity on the workflow designer and associate this activity with a method called MyActivity1. This association is done by entering the method name in the ExecuteCode property. In MyActivity1, we have displayed in the console that this is my first activity. Again, we have added one more code activity which points to MyActivity2. If you see the designer pane, we have sequenced code1 first and code2 next. In short, code1 will execute first and then code2. This is clear from the output displayed below.
  4. This is the code behind the workflow:

Image 4

Figure 4: Sequential Workflow Using Designer

How do we specify conditions in a workflow?

Yes, you can define conditions in a workflow using a conditionedActivitygroup. Below is the numbered snapshot which shows how to use a conditionedActivitygroup.

  1. You can see in this snapshot that we have defined a conditionedActivitygroup with two conditions. The two boxes inside the group define the two conditions.
  2. You can select one of the condition boxes and define the condition using the WhenConditions property. If this condition is true, you need to specify in the execute code which method to execute. For instance, in the current snapshot, we have said that the old1 method should execute if age > 21. The same procedure we need to follow for the second condition box. In the second condition box, we have specified to execute young1 method if age < 21. Currently, the second condition is not visible in the below snapshot.
  3. The workflow editor also provides a cool interface called as Rule Condition Editor, which can be used to specify conditions. Age is a public property in the behind code. You can also get Age in the intelligence of the rule condition editor.
  4. Both the conditions will execute inside the condition activity group. We need to specify when this conditionactivitygroup should exit. Therefore, we have a function called exit. If the user inputs age as -1, it will exit from the loop or it will take inputs from the user and continue evaluating depending on the two conditions.

Image 5

Figure 5: Sequential Workflow with Conditions

How do you handle exceptions in a workflow?

Exception handling in a workflow is somewhat different from how we do in normal .NET applications. Below is the numbered snapshot of how we can handle exceptions in a workflow.

  1. We have a small tab which says View Exceptions. If you click on View Exceptions, you will be redirected to a workflow design only for the exception as shown in the numbered snapshot 2.
  2. This is the workflow which will execute in case we have exceptions. We have put a code activity which points to a method called as raiseException. In case of exceptions in the workflow, this path will be followed.

Image 6

Figure 6: Workflow With Exception Handling

What is the use of XOML files?

Twist: How can we serialize workflows?

Windows Workflow Foundation gives developers a declarative way to create workflows using XAML. See the WPF chapter for more details about XAML. These markup files are stored with a XOML (Extensible Object Markup Language) extension. In the below snapshot you can see a Workflow1.xoml file created by the designer. The markup file can also have a code behind. The whole concept of having a code behind for a XOML file is to separate the presentation from the logic files. In the below code snapshot, we have made a simple XOML sample. Below is the explanation number wise:

  1. In order to create a XOML file, you need to add a sequential workflow with separation. Which means that the XOML file will be created with a behind code.
  2. Currently we have two activities: code3 and code1. Below is the XOML file contents:
<?Mapping XmlNamespace="ComponentModel" 
    Assembly="System.Workflow.ComponentModel" ?>
<?Mapping XmlNamespace="Compiler" 
    Assembly="System.Workflow.ComponentModel" ?>
<?Mapping XmlNamespace="Activities" 
    Assembly="System.Workflow.Activities" ?>
<?Mapping XmlNamespace="RuleConditions" 
    Assembly="System.Workflow.Activities" ?>
<SequentialWorkflow x:Class="WorkflowSeq.Workflow1" 
    x:CompileWith="Workflow1.xoml.cs" ID="Workflow1" 
    xmlns:x="Definition" xmlns="Activities">
<Code ExecuteCode="Mycode3" ID="code3" />
<Code ExecuteCode="Mycode1" ID="code1" />

See the above snippet of the XOML file. You can see how the behind code is linked using the CompileWith attribute. Code forms the element of the SequentialWorkflow tag. One of the best things with markup is we can change the sequence just by changing the XOML file, we do not need to compile the whole application again.

Image 7

Figure 7: XOML in Action

In the above snapshot, one of the things to know is the 3, 4, and 5 numbered sections. These sections are not linked with the sample. But just to make you aware, you can create and serialize any workflow and deserialize them again using the text writer object.

How can we pass parameters to a workflow?

When you call the start workflow function, you can pass as name / value pairs using the dictionary object.

Image 8

Figure 8: Passing a Value to a Workflow

For further reading do watch the below interview preparation videos and step by step video series.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India

Comments and Discussions

GeneralRe: I don't get the point of WF Pin
paillave26-Feb-09 22:49
paillave26-Feb-09 22:49 
GeneralRe: I don't get the point of WF Pin
Razan Paul (Raju)27-Feb-09 2:22
Razan Paul (Raju)27-Feb-09 2:22 
QuestionRe: I don't get the point of WF Pin
Ankita Biswas Bhattacharya18-Apr-16 22:04
Ankita Biswas Bhattacharya18-Apr-16 22:04 
GeneralRe: I don't get the point of WF Pin
Martin Lercher6-Oct-08 0:59
Martin Lercher6-Oct-08 0:59 
GeneralRe: I don't get the point of WF Pin
chaiguy13376-Oct-08 5:12
chaiguy13376-Oct-08 5:12 
QuestionASP.Net? Pin
sleupold29-Sep-08 8:14
sleupold29-Sep-08 8:14 
AnswerRe: ASP.Net? Pin
Shivprasad koirala30-Sep-08 6:21
Shivprasad koirala30-Sep-08 6:21 
GeneralRe: ASP.Net? Pin
E! Ray K2-Oct-08 14:53
E! Ray K2-Oct-08 14:53 
seem like overkill to me, if you ask me

Go with the flow...

GeneralGreat Information! Pin
LastZolex22-Sep-08 21:16
LastZolex22-Sep-08 21:16 
GeneralWPF FAQ's Pin
Aslesh22-Sep-08 2:57
Aslesh22-Sep-08 2:57 
GeneralRe: WPF FAQ's Pin
Shivprasad koirala22-Sep-08 6:26
Shivprasad koirala22-Sep-08 6:26 
Generalnice article Pin
shahed hasan21-Sep-08 21:41
shahed hasan21-Sep-08 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.