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