Working with Windows Workflow Foundation 4

Introduction

Windows Workflow Foundation provides a simplified programming model, an in-process workflow engine and improved designer that can be used to design and implement workflows in .NET applications. This article discusses the new features in Windows Workflow Foundation 4 and shows how you can get started with it using Microsoft Visual Studio 2010 IDE.

Pre-requisites

To execute the code examples illustrated in this article, you should have Microsoft Visual Studio 2010 installed in your system.

Before we delve into the new features and enhancements in Windows Workflow Foundation 4 and get started working with it in Microsoft Visual Studio 2010, let's take a quick tour of some important concepts.

Business Process, Workflow, Windows Workflow Foundation

A business process is a collection of activities that have a common objective. The Wikipedia states: "A business process or business method is a collection of related, structured activities or tasks that produce a specific service or product (serve a particular goal) for a particular customer or customers. It often can be visualized with a flowchart as a sequence of activities." Reference: http://en.wikipedia.org/wiki/Business_process

Microsoft' Windows Workflow Foundation is a framework that can be used to create, manage and execute workflows. A Workflow in the context of Windows Workflow Foundation may be defined as a tool that is used to model the flow of data and activities that relate to the actions to be performed on the data. According to the Workflow Management Coalition, workflow is defined as the "automation of a business process, in whole or part, during which documents, information, or tasks are passed from one participant to another for action, according to a set of procedural rules."

Reference: www.wfmc.org/ .

Workflows are a good choice for any application that has one or more long running process. Workflows enable you to change behaviour without much change in the application's code. For applications that thrive on business rules that can change over a period of time, workflows are a great choice.

A Workflow Management System (commonly known as WFMS) is software that manages workflow execution, by interpreting the process definition, interacting with workflow participants and, where required, invoking external IT tools and applications. Note that all workflow systems are process oriented.

Workflow Management Systems have two basic components: the workflow modeling component and the workflow execution component. While the former can be used to model workflows using a visual designer, the later is used to execute and manage your workflows.

Benefits of Workflow

Automating workflows provides a lot of benefits - these include:

  • Reduced operating cost
  • Increased efficiency
  • Better flexibility, adaptability and control

Types of Workflow

Windows Workflow Foundation 4 supports the following types of Workflow:

  1. Sequential - This is a type of Workflow in which the actions are executed in a predefined sequence. A typical example of a sequential Workflow is the sequence of steps needed to connect to a database in ADO.NET Entity Framework
  2. State Machine - This is a type of Workflow in which there aren't any predefined sequence. Rather, the Workflow is represented using states and the transitions between such states. Note that State Machine workflows are no longer supported by Windows Workflow 4

Architectural Components of Windows Workflow Foundation

The major architectural components of Windows Workflow Foundation include:

  1. Base Activity Library - This comprises of a collection of base classes, activities, etc. These base classes can be extended to create custom activities.
  2. Runtime Engine - This is the component that provides support for managing and executing workflows.
  3. Runtime Services - This is the component that provides support for hosting workflows.

New Features in Windows Workflow Foundation 4

Windows Workflow Foundation is a Microsoft framework that can be used to create, manage and execute workflows in a managed environment. Windows Workflow 4 is available as part of .NET Framework 4.0 and ships with Microsoft Visual Studio 2010. The most important feature of Windows Workflow 4 is its ability to be tightly integrate with Windows Communication Foundation. Windows Workflow Foundation 4 is more or less a complete rewrite of the Workflow library. It uses XAML to define workflows and runs on top of .NET Framework 4.0. The other major enhancements to Windows Workflow 4 include:

  1. State Machine Workflows are no longer supported - With WF 4, you no longer have support for creating state machine workflows.
  2. Excellent integration with Windows Communication Foundation 4 - There has been significant improvements to WCF and WF in .NET Framework 4. New features added in WF 4 for improved integration with WCF applications include: message correlation, better support for hosting, etc.
  3. Support for Extended Base Class Library - Windows Workflow Foundation 4 now contains an Extended Base Class Library that contains a list of new activities added. These activities include: Flowchart, ForEach, DoWhile, Break (in the Flowchart group) and Send, Receive, SendReceive, RecieveParameters, CorrelationScope, InitializeCorrelation (in the WCF group).
  4. Simplified Programming Model - With Window Workflow 4, creating, hosting, managing and executing workflows have been made much simpler.
  5. Designer improvements - The Workflow designer has been improved a lot in WF 4. You can now create, manage and execute much complex workflows with no degradation in performance.



Working with Windows Workflow Foundation 4

Getting started with Windows Workflow Foundation 4

To get started with your first Windows Workflow 4 application in Microsoft Visual Studio 2010, follow these steps:

  1. Open Microsoft Visual Studio 2010 IDE
  2. Click on File->New->Project
  3. Select "Workflow" as the group of the project templates displayed
  4. From the Workflow templates displayed, select "Workflow Console Application"
  5. Specify a name for your project and click OK to save as shown in Figure 1 below

    [Figure 1.png]
    Figure 1: Creating a new Workflow 4 Application

    If you observe in the Solution Explorer Windows, you will see two files created, Program.cs and Workflow1.xaml
  6. Rename the file Workflow1.xaml to CodeGuruWorkflow.xaml and change all references accordingly

    The Solution Explorer Window now looks as shown in Figure 2 below:

    [Figure 2.png]
    Figure 2: The CodeGuruWorkflow in the Solution Explorer Window

    In the Toolbox you can see a collection of activities grouped by their categories. Figure 3 illustrates these activities grouped by their categories.

    [Figure 3.png]
    Figure 3: Workflow Activities grouped by categories in the Toolbox

  7. Select WriteLine activity from the Toolbox as shown in Figure 3 above

    The purpose of the WriteLine activity is to write a line of text to the output stream. The WriteLine activity contains three properties, i.e., DisplayName, Text and TextWriter. The DisplayName property is used to specify the name of the Workflow as seen in the design surface. The Text property contains the text you would like the Workflow to print. The TextWriter property is optional and denotes the text stream to which the output would be written to. If you don't specify any value in it, the default is Console.Out.

  8. Drag and drop the WriteLine activity onto the design surface where the message "Drop activity here" appears. [Figure 4.png]
    Figure 4: The WriteLine activity and its properties

If you observe the property sheet, you'll see that there is a TextBox with the text, "<enter an expression>". Note that you should type in a Visual Basic statement here only even if you are using C# programming to develop your Workflow. You can enter either an expression or a text string. We typed "Hello World!" here.

Note that you no longer have the class called WorkflowRuntime in Windows Workflow 4 - you'll need to make use of WorkflowInstance to create an instance of your workflow. The Workflow would be invoked by the WorkflowInvoker.Invoke() method inside of the main method in the Program.cs file.

  using System.Activities;
  using System;
  
  namespace GettingStarted
  {
      class Program
      {
          static void Main(string[] args)
          {
              WorkflowInvoker.Invoke(new CodeGuruWorkflow());
              Console.Read();
          }
      }
  }

As a last step, execute the application by pressing F5. Here's how the output would look like:

[Figure 5.png]
Figure 5: The CodeGuruWorkflow in action!

Summary

You can use Windows Workflow Foundation to design and model your complex business problems using a visual designer. Then, these workflows can be hosted and executed in a managed environment. This article presented the new features in Windows Workflow Foundation 4 and discussed how we can get started working with it. It also discussed the basic concepts related to workflows, business processes, etc. Happy reading!

Suggested Readings

http://blogs.msdn.com/b/usisvde/archive/2010/01/10/first-look-at-workflow-in-net-4.aspx
http://msdn.microsoft.com/en-us/magazine/cc163640.aspx
http://www.informit.com/articles/article.aspx?p=418010&seqNum=4
http://www.soamag.com/I29/0509-3.php
http://www.devx.com/architect/Article/33059/1954

Related Articles





About the Author

Joydip Kanjilal

Microsoft Most Valuable Professional, Author and Speaker. Featured in "MSDN Featured Developer of the Fortnight (India)" a number of times. Winner of Community Credit Awards at www.community-credit.com several times. Authored numerous books and articles in Microsoft .NET and its related technologies. Authored the following books:-- ASP.NET 4.0 Programming (Mc-Graw Hill Publishing) Entity Framework Tutorial (Packt Publishing) Pro Sync Framework (APRESS) Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing) ASP.NET Data Presentation Controls Essentials (Packt Publishing)

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds