Sunday, August 27, 2006 2:18 AM bart

Getting started with Windows Workflow Foundation (WF)

Windows Workflow Foundation (WF) is one of the key pillars of the .NET Framework 3.0 (formerly known as WinFX), next to Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF) and Windows CardSpace (WCS). If you haven't done so, download the July CTP now.

A personal opportunity

Now, why am I telling you this? Historically, I've been avoiding BizTalk-related stuff just to be able to concentrate on the basic stuff I'm usually spending time with, such as CLR, C# language features, the BCL, SQL Server, web services, etc. Learning to work with another server product like BizTalk implies yet another learning curve and time was rather limited. But enough boring excuses.

Workflow is more widely accepted than a few years ago and is growing up to become (if it isn't already) a mainstream paradigm in software development. Combine this with the opportunity to write a university thesis on workflow (at UGent) and guess what Bart said to himself a couple of months ago? Indeed, time to enter the world of Windows Workflow Foundation. The research subject is entitled "Dynamic and Generic Workflows with WF", quite a broad definition with a lot of freedom. One of the things that will be researched is how to adapt workflows dynamically under various (stress) conditions. So, stay tuned for additional information on WF-related experiments.

Reading resources

In the meantime I recommend the following book, which was handed out at the PDC 05 last year. It's already quite outdated due to "CTP madness" (well, I should say "evolution", we're talking about technologies which are still under development after all, although the end is near, very near) but it's the only book I know of so far that's publicly available:

Other books that will appear in the next couple of months include:

Hello, Workflow!

Because I want to stress the "getting started" nature of this blog post, let's do a trivial thing. To all WF newbies: welcome to your first WF exposure.

Before you read on, make sure to have downloaded and installed the .NET Framework 3.0, the Windows SDK and the Visual Studio 2005 Extensions for WF. More information can be found on http://msdn.microsoft.com/windowsvista/downloads/products/getthebeta/default.aspx.

Step 1 - A new workflow project

Open Visual Studio 2005 and create a new project. Choose for a Sequential Workflow Console Application:

Let's call it HelloWorkflow to illustrate how big our imagination really is :$. In the solution explorer, delete Workflow1.cs. Although this is not necessary it's interesting to create the workflow in another way, using "code separation" with XOML (which, according to Wikipedia, stands for eXtensible Object Markup Language).

Step 2 - Add a workflow with code separation

Add a new item to the project and choose for Sequential Workflow (with code separation) and call it HelloWorkflow. As you can see, it has the extension .xoml:

Step 3 - Defining the workflow

Workflows consist of activities that are executed in a well-defined order by the workflow runtime engine. These activities can be found in the Visual Studio 2005 toolbox, but it's also possible to create your own activities e.g. by composition (just as you can create a new Windows Forms control by defining a User Control consisting of other "orchestrated" controls). The toolbox looks as follows:

For the sake of the demo, drag and drop a Code(Activity) to the "Drop Activities to create a Sequential Workflow" part in the designer. Using the properties grid, change the name of the new activity from codeActivity1 to sayHello. The result should look like this:

The red exclamation mark tells you there's still something wrong with the activity's configuration. More specifically, the designer tells you "Property 'ExecuteCode' is not set.". Basically, ExecuteCode is an event handler which can be created by double-clicking on the code activity. (Note: for other activities too, always use the red exclamation mark assistance to configure an activity properly). The code for our activity will consist of a simple Console.WriteLine call:

namespace HelloWorkflow
{
   public partial class HelloWorkflow :
SequentialWorkflowActivity
   {
      private void sayHello_ExecuteCode(object sender, EventArgs
e)
      {
         Console.WriteLine("Hello, Workflow!"
);
      }
   }
}

Notice the use of a partial class. The other part of the HelloWorkflow definition lives in the .xoml file. Basically XOML (and XAML too) is just some chunk of XML describing a series of object instantiations, property setting stuff, nesting, etc. Tip: right-click the HelloWorkflow.xoml file in the Solution Explorer, choose Open With... and select the XML Editor:

<SequentialWorkflowActivity x:Class="HelloWorkflow.HelloWorkflow" x:Name="HelloWorkflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
   <
CodeActivity x:Name="sayHello" ExecuteCode="sayHello_ExecuteCode"
/>
</
SequentialWorkflowActivity>

Basically this code comes down to:

namespace HelloWorkflow
{
   public HelloWorkflow : SequentialWorkflowActivity
   {
      public HelloWorkflow()
      {
         CodeActivity sayHello = new CodeActivity();
         sayHello.ExecuteCode += new EventHandler(this.sayHello_ExecuteCode);

         this.Activities.Add(sayHello);
      }
   }
}

Step 4 - Hosting the workflow engine

In order to execute a workflow, one needs to host the workflow engine. However, no worries: Visual Studio 2005 has generated the code for us in Program.cs. It needs a little modification however because we did remove Workflow1.cs and replaced it by HelloWorkflow.cs. Here's the modified Main code (change indicated in bold):

static void Main(string[] args)
{
   using(WorkflowRuntime workflowRuntime = new WorkflowRuntime
())
   {
      AutoResetEvent waitHandle = new AutoResetEvent(false
);
      workflowRuntime.WorkflowCompleted +=
delegate(object sender, WorkflowCompletedEventArgs
e) {waitHandle.Set();};
      workflowRuntime.WorkflowTerminated +=
delegate(object sender, WorkflowTerminatedEventArgs
e)
      {
         Console
.WriteLine(e.Exception.Message);
         waitHandle.Set();
      };

      WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorkflow
));
      instance.Start();

      waitHandle.WaitOne();
   }
}

This code initializes the WorkflowRuntime and starts the workflow by calling the CreateWorkflow method. The anonymous method plumbing keeps the console application alive till the workflow has executed and also reports exceptions that terminated the workflow.

Step 5 - Run run run

Time to hit CTRL-F5 and to see Windows Workflow Foundation come alive:

What's coming up next?

In a next WF episode I'll tell you more about making dynamic changes to a running workflow. Other topics that will be covered include workflow persistence (dehydration and rehydration of a workflow), combining workflow with web services and combining WCF with WF.

Stay tuned!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

# Windows Workflow Foundation

Thursday, August 31, 2006 10:26 AM by ScottGu's Blog

Workflow is one of the new core capabilities (along with WPF aka Avalon and WCF aka Indigo) being added

# WF: Windows Workflow Foundation

Thursday, August 31, 2006 3:08 PM by while(availableTime>0) {

Yeah, I know that&#180;s a strange acronym and that it should be WWF, but what would it be of WWF , then?...

# http://weblogs.asp.net/scottgu/archive/2006/08/31/windows-workflow-foundation.aspx

Friday, September 01, 2006 3:15 AM by TrackBack

# Windows Workflow Foundation

Friday, September 01, 2006 8:44 AM by Tom's corner

# Workflow Foundation Learning - I DID IT!

Saturday, September 16, 2006 11:59 PM by while(availableTime>0) {

Well, today (4th of September 2006, almost the independence day of my country) I decided to learn about...

# re: Getting started with Windows Workflow Foundation (WF)

Sunday, October 08, 2006 8:16 AM by chandrashekar

when i try to open .xoml file in design view iam gettint this error

"method   'GetSelectedPropertyContext' in type Microsoft.WorkFlow.VSDesigner.ExtendedUIService' from assembly 'Microsoft.WorkFlow.VsDesigner version=3.0.0.0, Culture=neutral, publicKeyToken=31bf3856ad364e351 does not have implementation"

# 2006 October 09 &laquo; Angel &#8220;Java&#8221; Lopez on Blog

Monday, October 09, 2006 11:42 AM by 2006 October 09 « Angel “Java” Lopez on Blog

# Learning Windows Workflow Foundation &laquo; Angel &#8220;Java&#8221; Lopez on Blog

# Aprendiendo Windows Workflow Foundation

Monday, October 09, 2006 11:58 AM by Angel "Java" Lopez

En estos dias, he ordenado algunos enlaces y recursos sobre el Windows Workflow Foundation, el motor

# Workflows in MOSS @ MOSSIG &laquo; Grumpy Wookie

Wednesday, April 18, 2007 6:12 AM by Workflows in MOSS @ MOSSIG « Grumpy Wookie

# Workflows in MOSS - OOB vs. SPD vs. WF vs. k2.net (battle of the acronyms) &laquo; devK2.net

# A quick search &laquo; .NET according to Bruce

Saturday, July 21, 2007 7:35 AM by A quick search « .NET according to Bruce

Pingback from  A quick search &laquo; .NET according to Bruce

# Windows Workflow &laquo; GreenRock Software Code Comment

Wednesday, June 04, 2008 7:09 PM by Windows Workflow « GreenRock Software Code Comment

Pingback from  Windows Workflow &laquo; GreenRock Software Code Comment

# Windows Workflow &laquo; GreenRock Software Code Comment

Wednesday, June 04, 2008 7:10 PM by Windows Workflow « GreenRock Software Code Comment

Pingback from  Windows Workflow &laquo; GreenRock Software Code Comment

# Iulian Tab??r??: Blog &raquo; Blog Archive &raquo; Windows Workflow Foundation

Pingback from  Iulian Tab??r??: Blog  &raquo; Blog Archive   &raquo; Windows Workflow Foundation

# Getting started with Windows Workflow Foundation (WF) - B# .NET Blog | WWF Tutorial | Scoop.it

Pingback from  Getting started with Windows Workflow Foundation (WF) - B# .NET Blog | WWF Tutorial | Scoop.it