Peter Goodman bio photo

Peter Goodman

A developer living in Auckland building software using all sorts of bits and pieces I find along the way. Originally from Northern Ireland.

Twitter Google+ LinkedIn Github

I thought I’d do a series of posts on workflow services. There are plenty of resources out there already on how to build workflows and activities although not a huge amount has been said about workflow services. In my opinion this is the real power of workflow and its where WF4 really comes into its own. In fact I’m going to presume that you’ve played a little with workflow and understand what an activity is, that’s about it.

Over the series we’re going to be building a calculator service which performs integer operations in a stateful way so that subsequent operations build on the previous output.

1. Open Visual Studio 2010

2. Create a New Project of Type Visual C#/Workflow/WCF Workflow Service Application

CropperCapture[3]

3. The project template should create the following workflow for you.

CropperCapture[4]

So what are these activities? Well if you’ve read any other intros to workflow you will know that the outer activity is a Sequence. Simply put, the child activities enclosed in a sequence are executed sequentially, of course. The activities labelled ReceiveRequest and SendResponse are Receive and SendReply activities. Typically found together these activities are the basis of creating any workflow service. Each pair represents an operation on a service contract. The operation name as shown here is GetData and the “Content” on each of the activities is the definition of the Request or Response message.

4. Next we’re going to change the operation name and the message contents so that we get an Start operation for our Calculator service which accepts and integer.

The Start operation is going to take an integer value and begin our calculator service. From this integer we can then perform as many operations as we like acting on the current state. To store the current state we need to first create a “Value” variable. Click on the Variables button at the bottom of the designer and add a new Int32 variable called “Value”. You can delete the data variable as we will not need it.

CropperCapture[5]

On the ReceiveRequest activity change the OperationName to “Start”. Click View Message next to the Content of the ReceiveRequest activity, change the mode to “Parameters” and add a new parameter called value of type integer and assign it to our “Value” variable.

CropperCapture[6]

On the “SendResponse” activity change the message content to “Parameters” and add a “Result “ parameter with the value as below:

CropperCapture[7]

5. Run the service by hitting CTRL+F5, this will start the WCF Test Client. Double click the Start method on the left hand pane and enter an integer value then Invoke. You should see the result of our service operation as below;

CropperCapture[9]

You can notice that we have created a service with the contract name “IService” which has a single operation “Start” accepting an integer value and returning a string. This is really no different than creating a normal WCF service with a contract interface and an implementation class that returns a string like the following:

public class Service1 : IService1 {
    public string Start(int value) {
        return string.Format("Calculation session started with value {0}", value);
    }
}

[ServiceContract]
public interface IService1 {

    [OperationContract]
    string Start(int value);
}

So how do we change the contract name? Click the Receive activity and then on the Properties panel (F4) change the ServiceContractName property to another namespace and name combination.

So that was how to quickly create a WF4 service. Next we’ll add the calculation operations and do some correlation.

The source for the steps in this post is attached below.