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!



[f1]
[f2]

2 thoughts on “Using Windows Workflow Foundation 4 Receive from an non WF client

  1. Can you explain why it is necessary to use MessageContracts when not using another workflow? Or can you point me towards something that explains it, please?

  2. Tmessages are returned un such a way that that the IsWrapped = false is required, by default they are expected to be wrapped. You cannot do this on a DataContract but you can on a MessageContract hence the requirement for a MessageContract. When WF is used on both sides the message is no different, it just takes care of things for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>