Using Windows Workflow Foundation 4 Receive from an non WF client
In a previous blog post I described how to use the WorkflowServiceHost and host a workflow with a Receive activity that waits for WCF messages. I also added a WF4 client that called the service and received a response. However a lot of clients out there are not going to be workflows but “regular” code that calls into out workflow. So what does it take to have a simple console application talk to our WF4 service?
The first thing we need is an service contract describing the operation we can call. In our workflow the operation is named “Operation1” and our service contact name was “MyService” with the default namespace of “http://tempuri.org”. So our service contract looks like this:
[ServiceContract]
interface MyService
{
[OperationContract]
Operation1Response Operation1(Operation1Request request);
}
Next up are the request and response objects. In this case I am using just two strings so they are pretty simple as well.
[MessageContract(IsWrapped = false)]
public class Operation1Request
{
[MessageBodyMember(
Namespace = "http://schemas.microsoft.com/2003/10/Serialization/",
Name = "string")]
public string Value { get; set; }
}
[MessageContract(IsWrapped = false)]
public class Operation1Response
{
[MessageBodyMember(
Namespace = "http://schemas.microsoft.com/2003/10/Serialization/",
Name = "string")]
public string Value { get; set; }
}
The interesting to note here is that I have to use a MessageContact instead of a DataContract and I have to make sure the data isn’t wrapped in an extra element. Also interesting is the fact that the data is send across using the name “string”, not the easiest C# identifier to work with so I called them “Value” instead.
Using these WCF definitions creating a simple console app that calls the workflow is a breeze.
static void Main(string[] args)
{
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://localhost:8090/Sequence1/Operation1");
var factory = new ChannelFactory<MyService>(binding, endpoint);
var proxy = factory.CreateChannel();
var request = new Operation1Request()
{
Value = "Test from a console."
};
var result = proxy.Operation1(request);
Console.WriteLine(result.Value);
Console.WriteLine("Regular client is done.");
Console.ReadLine();
}
Enjoy!
www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu