Click here to Skip to main content
Click here to Skip to main content

Windows Workflow Foundation FAQ

, 5 Jul 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A Windows Workflow Foundation FAQ

Table of contents

Introduction

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.

I have been writing and recording videos for architectures. I have uploaded some sample videos for Design Patterns, UML, and function points at http://www.questpond.com/FreeDesign1.htm. You can visit http://www.questpond.com and download the complete architecture interview questions PDF which covers SOA, UML, Design Patterns, Togaf, OOPs, etc.

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.

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.

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.

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:

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.

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.

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" 
    ClrNamespace="System.Workflow.ComponentModel" 
    Assembly="System.Workflow.ComponentModel" ?>
<?Mapping XmlNamespace="Compiler" 
    ClrNamespace="System.Workflow.ComponentModel.Compiler" 
    Assembly="System.Workflow.ComponentModel" ?>
<?Mapping XmlNamespace="Activities" 
    ClrNamespace="System.Workflow.Activities" 
    Assembly="System.Workflow.Activities" ?>
<?Mapping XmlNamespace="RuleConditions" 
    ClrNamespace="System.Workflow.Activities.Rules" 
    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" />
</SequentialWorkflow>

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.

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.

Figure 8: Passing a Value to a Workflow

License

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

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
QuestionCan we use Windows Workflow Foundation in Web application(ASP .Net application) PinmemberMember 1081540714-Dec-14 21:14 
QuestionRequest Approval State Machine Workflow with Persistance example PinmemberMember 98323036-Jan-14 1:57 
GeneralMy vote of 5 Pinmemberfereshteh_h27-Jan-13 0:32 
Generalmy vote of 5 PinmemberUday P.Singh22-Nov-11 22:12 
SuggestionGood Article PinmemberT. Abdul Rahman8-Oct-11 0:55 
QuestionHow to create generic WF? PinmembereOrdinary23-Sep-09 21:54 
I want to create a workflow which will be called from ASP.NET as well as Windows service. Can you please guide me how to create it?
GeneralGot a four from me PinmemberLee Humphries4-Jun-09 14:12 
GeneralRe: Got a four from me PinmemberShivprasad koirala4-Jun-09 18:04 
GeneralI was looking for a quick reference for WWF, and here it is. Pinmembermisaxi3-Jun-09 22:54 
GeneralVery good article PinmemberDonsw16-Apr-09 6:14 
GeneralI have a question for you Pinmemberh_CodeProject27-Feb-09 0:01 
GeneralRe: I have a question for you PinmemberMarcelo Lopez27-Feb-09 5:07 
GeneralRe: I have a question for you PinmemberThaoP27-Feb-09 6:16 
GeneralWWF + SP PinmemberAlex Koshelev7-Oct-08 5:05 
GeneralI don't get the point of WF Pinmemberchaiguy133730-Sep-08 12:05 
GeneralRe: I don't get the point of WF PinmemberStockportJambo30-Sep-08 13:58 
GeneralRe: I don't get the point of WF Pinmemberchaiguy133730-Sep-08 14:16 
GeneralRe: I don't get the point of WF Pinmemberchaiguy13376-Oct-08 6:14 
GeneralRe: I don't get the point of WF Pinmemberpaillave26-Feb-09 23:49 
GeneralRe: I don't get the point of WF PinmemberRazan Paul (Raju)27-Feb-09 3:22 
GeneralRe: I don't get the point of WF PinmemberMartin Lercher6-Oct-08 1:59 
GeneralRe: I don't get the point of WF Pinmemberchaiguy13376-Oct-08 6:12 
QuestionASP.Net? Pinmembersleupold29-Sep-08 9:14 
AnswerRe: ASP.Net? PinmemberShivprasad koirala30-Sep-08 7:21 
GeneralRe: ASP.Net? PinmemberE! Ray K2-Oct-08 15:53 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 6 Jul 2009
Article Copyright 2008 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid