4 min read

 

Microsoft Windows Workflow Foundation 4.0 Cookbook

Microsoft Windows Workflow Foundation 4.0 Cookbook

Over 70 recipes with hands-on, ready to implement solutions for authoring workflows

  • Customize Windows Workflow 4.0 applications to suit your needs
  • A hands-on guide with real-world illustrations, screenshots, and step-by-step instructions
  • Explore various functions that you can perform using WF 4.0 with running code examples
  • A hands-on guide with real-world illustrations, screenshots, and step-by-step instructions

Read more about this book

(For more resources on this subject, see here.)

Introduction

Considering workflow programs as imperative program, we need to think of three fundamental things:

  • How to define workflow programs
  • How to build (compile) workflow programs
  • How to execute workflow programs

In WF4, we can define a workflow in either managed .NET code or in XAML. There are two kinds of code workflow authoring styles:

  • Custom Activity class
  • Creating workflow dynamically in the runtime

There are also two ways to author workflow in XAML:

  • By WF designer (recommended)
  • Typing XML tags manually

Essentially, workflow program is .NET program, no matter how we create it.

After defining workflows, we can build workflow applications as we build normal .NET applications.

When it comes to workflow execution, we need to consider three basic things:

  • How to flow data into and out of a workflow
  • How to store a temporary data when workflow is executing
  • How to manipulate data in workflow

This article is going to focus on answering these questions.

Before moving ahead, make sure we have the following installed on our computer:

  • Windows Vista/7 or Windows Server 2008
  • Visual Studio 2010 and .NET framework 4.0

We can also use Windows XP; however, its usage is not recommended.

Creating the first WF program: HelloWorkflow

In this task we will create our first workflow to print “Hello Workflow” to console application.

How to do it…

  1. Create a Workflow Console Application project:
    After starting Visual Studio 2010, select File | New Project. A dialog is presented, as shown in the following screenshot. Under Visual C# section, select Workflow, and choose Workflow Console Application. Name the project HelloWorkflow. Name the solution Chapter01 and make sure to create a directory for the solution.

  2. Author workflow program:
    First, drag a Sequence activity to designer from Toolbox, next drag a WriteLine activity into Sequence activity. Finally, input “Hello Workflow” in the expression box of WriteLine activity. We can see the following screenshot:

  3. Run it:
    Press Ctrl+F5 to run project without debugging. The result is as shown in the following screenshot:

How it works…

When we press Ctrl+F5, Visual Studio saves the current project, and then it runs the project from Main method in Program.cs file.

WorkflowInvoker.Invoke(new Workflow1());

The preceding statement starts the workflow. After the workflow starts running, WriteLine activity prints the “Hello Workflow” to Console Application.

The workflow we created in WF designer is actually an XML file. We can open Workflow1.xaml with XML editor to check it.

Right-click on Workflow1.xaml then click Open With…, and choose XML Editor to open the Workflow1.xaml as XML file.

All XAML files will be compiled to .dll or .exe file. That is why when we press Ctrl+F5, the program just runs like a normal C# program.

There’s more…

So far, there are no officially published WF4 Designer add-ins for Visual Studio 2008. We need a copy of Visual Studio 2010 installed on our computer to use WF4 designer, otherwise we can only create workflows by imperative code or by writing pure XAML file.

Creating a WF program using C# Code

In this task, we will create the same “HelloWorkflow” function workflow using pure C# code, beginning from a Console Application.

How to do it…

  1. Create a Console Application project:
    Create a new Console Application project under Chapter01 solution. Name the project HelloCodeWorkflow. The following screenshot shows the Console Application new project dialog:

  2. Add reference to System.Activities assembly:
    By default, a new Console Application doesn’t have reference to System.Activities assembly, due to which we need to perform this step.
  3. Create workflow definition code:
    Open Program.cs file and change the code present as follows:

    Open Program.cs file and change the code present as follows:
    using System.Activities;
    using System.Activities.Statements;

    namespace HelloCodeWorkflow {
    class Program {
    static void Main(string[] args) {
    WorkflowInvoker.Invoke(new HelloWorkflow());
    }
    }

    public class HelloWorkflow:Activity {
    public HelloWorkflow() {
    this.Implementation = () => new Sequence {
    Activities = {
    new WriteLine(){Text="Hellow Workflow"}
    }
    };
    }
    }
    }

  4. Run it:
    Set HelloCodeWorkflow as StartUp project and press Ctrl+F5 to run it. As expected, the result should be just like the previous result showed.

How it works…

We use the following namespace:

using System.Activities;
using System.Activities.Statements;

Because WorflowInvoker class belongs to System.Activities namespace and Sequence activity, WriteLine activity belongs to System.Activities.Statements.

public class HelloWorkflow:Activity {
public HelloWorkflow() {
this.Implementation = () => new Sequence {
Activities = {
new WriteLine(){Text="Hellow Workflow"}
}
};
}
}

By implementing a class inherited from Activity, we define a workflow using imperative code.

WorkflowInvoker.Invoke(s);

This code statement loads a workflow instance up and runs it automatically. WorkflowInvoker.Invoke method is synchronous and invokes the workflow on the same thread as the caller.

There’s more

WF4 also provide us a class DynamicActivity by which we can create a workflow instance dynamically in the runtime. In other words, by using DynamicActivity, there is no need to define a workflow class before initializing a workflow instance. Here is a sample code:

public static DynamicActivity GetWF() {
return new DynamicActivity() {
Implementation = () => new Sequence() {
Activities ={
new WriteLine(){Text="Hello Workflow"}
}
}
};
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here