Angel \”Java\” Lopez on Blog

September 6, 2007

Windows Workflow hosted as a WCF Service

I wrote an example about using two new features of Windows Worflow Foundation shipped in .NET 3.5: the workflow can be hosted and exposed as a Windows Communication Foundation Service, and the use of Receive Activity. I used Visual Studio 2008 Beta 2, as the IDE. This post it’s not a detailed step by step. It pretends to highlight some procedures and concepts.

The code can be downloaded from HostingWorkflow1.zip.

It’s a minimalist example: a workflow instance is created, it waits for an external events, then it continues, writing a line to the console. Simpler, impossible.

The example contains a solution with:

– A console program that hosts and expose a sequential workflow.

– A client console program

– An interface, used by both programs

– A Sequential Workflow Library, with a Receive Activity, and a Code Activity.

This is the appearance of the final workflow design:

The workflow process an order, for a fictitious restaurant. First, I created a class library project, to define the common interface:

This interface define two methods: one to cook the order, and other to cancel it. The example only uses the cooked method (we’re optimistic), and nothing more. But in a more evolved example, we can include a create order method, and some decision branch in the workflow.

Note the using of System.ServiceModel, to mark with WCF attributes the interface and its methods.

In the Sequential Workflow Library, I initially dragged a Receive Activity:

It’s a new activity, that is in the revamped toolbox:

When you add the Receive Activity, it must be completed with a Service Operation Info (more about another important property in a following paragraph):

You can clicked on the ellipsis, to use a dialog:

Notably, you can create a contract, without write the interface. But, in this example, I choose to import the interface that I’d previously defined. Then, using the Import… button, you can select the interface, from the referenced assemblies (the workflow library project reference the class library project containing the interface definition) :

Pressing the OK button, you can select the method to use to invoke the Receive Activity:

The receive activity waits until an OrderCooked method invocation is received. This is a new way to manage the incoming events on a workflow. The interface contract will be exposed as a WCF service.

I added a code activity, after the receive activity, and wrote a simple code to write a message:

A host console program creates a Workflow Runtime, hosted under WCF:

Note the use of the new WorkflowServiceHost class, and the adding of a service endpoint to expose the order interface.

The client console program programmatically creates a WCF channel that use the interface, only to create a workflow instance, and make it advance, sending an OrderCooked method invocation:

You must notice that the program never create a workflow instance. Where is the instance created? Remember the Receive Activity: it has a property CanCreateInstance, and I set it to True:

In order to test the solution, I modify its properties, to launch the client and the host project at the same time:

And voila! Client outcome:

and Host outcome:

It’s not the ultimate workflow demo, but I hope that it will be useful to show these new features from .NET 3.5.

Thanks to Southworks and the Tamesis team, for the support to write this post.

Angel “Java” Lopez
http://www.ajlopez.com/

October 9, 2006

Learning Windows Workflow Foundation

Filed under: .NET, Windows Workflow Foundation — ajlopez @ 9:28 am

Recently, I collected some links and resources about Windows Workflow Foundation, the Microsoft workflow engine that is part of the upcoming .NET 3.0 platform. You can find more detailed information at

http://msdn.microsoft.com/workflow

The engine can run on any NET 2.0 installation. You need to download and install it from the above link. Currently, WWF is a set of new namespaces (System.Workflow.*), and utilities (like Workflow Designer) that you can run and use from Visual Studio 2005.

The site dedicated to WWF from Microsoft, is:

http://wf.netfx3.com/

If you want to study about this engine, I can suggest some links, from my developer link bag:

Windows Workflow Reference
http://msdn2.microsoft.com/en-us/netframework/aa663322.aspx

Windows Workflow Overview

You must read the documentation of the product at
Windows Workflow Foundation General Reference

The detailed list of classes at
Windows Workflow Foundation Class Library

In these  articles, Dino Esposito gives an introduction to WWF, with WinForm and ASP.NET examples:
Cutting Edge: Windows Workflow Foundation
Cutting Edge: Windows Workflow Foundation, Part 2

Hosting Windows Workflow
http://www.odetocode.com/Articles/457.aspx
WWF runs in a host. In this article, you’ll find a detailed explanation of the alternatives to host a WWF runtime.

From TheServerSide.net:

Enterprise _NET Community Manage Application Processes with Windows Workflow Foundation
http://www.theserverside.net/tt/articles/showarticle.tss?id=ManageAppProcesses

Will work for process: Create and run WF workflows
http://www.theserverside.net/tt/articles/showarticle.tss?id=WorkforProcess

A new article from Microsoft:
Web Service Workflows Deploy Distributed Business Processes With Windows Workflow And Web Services — MSDN Magazine, October 2006
http://msdn.microsoft.com/msdnmag/issues/06/10/WebServiceWorkflows/default.aspx

WWF can persist the status and tracks the workflow instance history. An interesting discussion at:
WWF Persistance Status
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=774811&SiteID=1

You can write state machine-based workflows:
State Machines In Windows Workflow
http://www.odetocode.com/Articles/460.aspx

Bart De Smet has a blog category on WWF, with articles:

Getting started with Windows Workflow Foundation (WF)
http://community.bartdesmet.net/blogs/bart/archive/2006/08/27/4278.aspx

How to make a workflow dynamic – Part 1
http://community.bartdesmet.net/blogs/bart/archive/2006/08/28/4322.aspx

How to make a workflow dynamic – Part 2
http://community.bartdesmet.net/blogs/bart/archive/2006/08/31/4369.aspx

Exposing a workflow through a web service
http://community.bartdesmet.net/blogs/bart/archive/2006/09/03/4388.aspx

Exposing a workflow via WCF
http://community.bartdesmet.net/blogs/bart/archive/2006/09/04/4392.aspx

Tom Lake, who worked on the WWF SDK, has a lot of information, about custom activities, XAML activation, and WWF in general. Some articles:

Assorted Windows Workflow  XAML activation and how to use it
http://blogs.msdn.com/tomlake/archive/2006/05/02/588756.aspx

Switch Activity
http://blogs.msdn.com/tomlake/archive/2006/08/30/Switch-activity.aspx

Basics of Working with Custom Types in Workflow
http://blogs.msdn.com/tomlake/archive/2006/07/17/668507.aspx

Assorted Windows Workflow  Do you want to invoke another workflow but you won’t know which one until runtime
http://blogs.msdn.com/tomlake/archive/2006/07/14/666287.aspx

Assorted Windows Workflow  Do you need to launch another process from your workflow
http://blogs.msdn.com/tomlake/archive/2006/07/14/666159.aspx

Assorted Windows Workflow  How to set activity property from a custom input form
http://blogs.msdn.com/tomlake/archive/2006/06/05/618185.aspx

Assorted Windows Workflow  Couple of custom activities that you can use to synchronize branches of a ParallelActivity
http://blogs.msdn.com/tomlake/archive/2006/05/04/590225.aspx

Assorted Windows Workflow  Have you had problems using the FileWatcher sample in a StateMachineWorkflow or WhileActivity
http://blogs.msdn.com/tomlake/archive/2006/05/02/588575.aspx

Assorted Windows Workflow  How to send data to a workflow
http://blogs.msdn.com/tomlake/archive/2006/06/02/614492.aspx

Assorted Windows Workflow  Project template for XAML Activation Console Application
http://blogs.msdn.com/tomlake/archive/2006/05/18/601271.aspx

Assorted Windows Workflow  Examples of using Persistence and Tracking in ASP_NET
http://blogs.msdn.com/tomlake/archive/2006/05/17/600143.aspx

Assorted Windows Workflow  Using XAML activation in ASP_NET with a StateMachineWorkflow
http://blogs.msdn.com/tomlake/archive/2006/05/08/592554.aspx

The workflow implementation has a rule engine. Some links about this subject:

Introduction to the Windows Workflow Foundation Rules Engine
http://msdn.microsoft.com/windowsvista/reference/default.aspx?pull=/library/en-us/dnlong/html/intWF_FndRlsEng.asp

Windows Workflow: Rules and Conditions
http://www.odetocode.com/Articles/458.aspx

WWF: Comparing WWF rules and the Microsoft Business Rule Engine
http://www.topxml.com/BizTalk-Rule-Engine/re-16296_WWF–Comparing-WWF-rules-and-the-Microsoft-Business-Rule-Engine.aspx

WF: Comparing WF rules and the Microsoft Business Rule Engine  
http://geekswithblogs.net/cyoung/articles/56488.aspx

Rules Examples
Examples from Microsoft. Some special ones:

External Ruleset Demo (beta 2.2)
http://wf.netfx3.com/files/folders/rules_samples/entry309.aspx
The External Ruleset Toolkit demonstrates how to store rulesets in an external SQL Server database and load them at runtime for use by a Policy activity.

Rules Driven UI
http://wf.netfx3.com/files/folders/rules_samples/entry819.aspx
This sample will demonstrate this capability by using rulesets to pre-populate fields, do cross validation and various calculations on a target Windows Form. We will also show rules features such as forward chaining in this sample.

About rule engines in general:

Use Rules In Your Applications
http://www.codeproject.com/useritems/Rules_In_Your_Apps.asp
A Rules Engine class library with source code and documentation

Using a Rules Engine to separate Business rules from the application
http://www.codeproject.com/useritems/Using_a_Rules_Engine.asp

Some interesting discussion at Microsoft forums:

Re Trouble with WebService and Persistence – MSDN Forums
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=292683&SiteID=1

Using the Rules Engine with complex data containers – MSDN Forums
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=320688&SiteID=1

Re 3 layered architecture – Where to house rules – MSDN Forums
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=263825&SiteID=1

More links about WWF

How to lock a workflow instance using SqlWorkflowPersistenceService?http://blogs.msdn.com/sonalic/archive/2006/04/28/586498.aspx

Managing Workflow’s Lifecycle
http://blogs.msdn.com/moustafa/archive/2006/03/02/542459.aspx

Microsoft Windows Workflow Foundation (WWF)
http://www.kainos.com/index.php?option=com_knowledge&task=view&id=29&Itemid=116

Ajax based Workflow Monitor
http://www.masteringbiztalk.com/blogs/jon/PermaLink,guid,a423e010-647b-4a32-8dfe-071bf6f43705.aspx

Angel “Java” Lopez
http://www.ajlopez.com/en

Create a free website or blog at WordPress.com.