Tuesday, 8 October 2013

Duplex Message Exchange Pattern in WCF with Example

Duplex Message Exchange Pattern in WCF

Duplex message exchange pattern is a way in WCF in which client and service both can send messages to each other independently. Duplex message exchange pattern also known as callback.

As both client and service need to be connected to exchange messages this patter is quite complicated and slower than other message exchange pattern.
To implement the duplex we have to create the separate interface and we have to create method which can be either request-reply or one way type.

Following are the steps to implement duplex service in wcf:-

Step 1:-

Open the visual studio->File->New->Website->WcfService

Step 2:-

Create interface which you use to call back from service to the client:-

[ServiceContract]
public interface IDuplexCheck
{
    [OperationContract(IsOneWay=true)]
    void showstatus(string res);

}

Step 3:-

Create the interface for the service in which you define your operation:-

[ServiceContract(CallbackContract=typeof(IDuplexCheck))]

public interface IService
{

    //IsOneWay=False represents the duplex communication
    [OperationContract(IsOneWay=false)]
   
    void dataupdated();
}


To implement the call back or duplex service we have to set IsOneWay=True which defines the duplex.  Apart from this add callbackContract with interface which you defined for call back.

Step 4:-

Now implement this interface into your service and inside the method create the instance of callback interface. And call your method which interacts with client during the code execution.

In this example I declared showstatus method in which I am simply passing the following message and put this method into loop which executes 3 times.

public class Service : IService
{
   
    public void dataupdated()
    {

        for (int x = 0; x < 3; x++)
        {

            IDuplexCheck idc = OperationContext.Current.GetCallbackChannel<IDuplexCheck>();
            idc.showstatus("Call back request from Service  "+x+" Time");
       
        }
   
    }
}

Step 5:-

To implement the duplex service we have to implement the wsDualHttpBinding in the following manner:-

<services>
      <service name="Service">
        <endpoint address="" binding="wsDualHttpBinding" contract="IService"/>
      </service>
    </services>

Now execute the code



Figure 1

Step 6:-

Now add service reference at your client side. At client side we have to implement IServiceCallBack interface which represent the interface we created at the service to achieve the call back functionality. And implement the method which we declared in that interface. The input parameter which we are using actually the result of service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;

public partial class _Default : System.Web.UI.Page,ServiceReference1.IServiceCallback
{
  
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            InstanceContext ic = new InstanceContext(this);
            ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient(ic);
            svc.dataupdated();
        }
       
    }

   //implementation of method which declared at Service Level in IDuplexCheck interface
    public void showstatus(string res)
    {

        Label1.Text = Label1.Text+ res+"<br/>";
       
    
   
    }
  
}
The output of this code is as follows:-




Figure 2

for any query send mail at info@techaltum.com


1 comment: