Saturday, August 30, 2014

Consume Workflow Service 4.5 from client without adding Service Reference

I came into a situation where I need to host the Workflow Service programmatically on the server and consumer application should be able to consume the application without adding the Service Reference.
I went through lot of articles and blogs on internet.
I divided this requirement in two parts.
  •  Dynamic hosting of workflow service
  •  Call Workflow Service without adding service reference.

Here is the sample for point 2 with default Workflow.
WF4 Service is just another WCF Service from client prospective.
Create a Proxy for Workflow Service at client side
First need to create the Service Contract same as we have in Workflow Service
[ServiceContract(Name = "IService")]
interface IMyService
{
       [OperationContract]
       GetDataResponse GetData(GetDataRequest request);
}

Now need to define the request and response messages using Message Contract.

Here we need to take care about one thing which is most important.
Open the Xamlx file in XML view and look the Request and Response Namespace. Your Namespace should match with your Xamlx file Namespace. For me it’s coming http://schemas.microsoft.com/2003/10/Serialization/

[MessageContract(IsWrapped = false)]
class GetDataRequest
{
   [MessageBodyMember(Name = "int", Namespace = "http://schemas.microsoft.com/2003/10/Serialization/")]
    public int Value { get; set; }
}
[MessageContract(IsWrapped = false)]
class GetDataResponse
{
    [MessageBodyMember(Name = "string", Namespace = "http://schemas.microsoft.com/2003/10/Serialization/")]
    public string Value { get; set; }
}

Now proxy is ready. We need to use this proxy to call the Workflow Service
var factory = new ChannelFactory<IMyService>(new BasicHttpBinding(), new EndpointAddress("http://localhost/Service1.xamlx"));
    var proxy = factory.CreateChannel();
    var response = proxy.GetData(new GetDataRequest() { Value = 1 });
}
Your Endpoint Address should match with your Workflow Service URL.

In the response you would be able to get the returned value by Workflow Service in response.

3 comments: