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:-
Open the visual studio->File->New->Website->WcfService
Create interface which you use to call back from service to the client:-
public interface IDuplexCheck
void showstatus(string res);
Create the interface for the service in which you define your operation:-
public interface IService
//IsOneWay=False represents the duplex communication
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.
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");
To implement the duplex service we have to implement the wsDualHttpBinding in the following manner:-
<endpoint address="" binding="wsDualHttpBinding" contract="IService"/>
Now execute the code
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.
public partial class _Default : System.Web.UI.Page,ServiceReference1.IServiceCallback
protected void Page_Load(object sender, EventArgs e)
InstanceContext ic = new InstanceContext(this);
ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient(ic);
//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:-
for any query send mail at firstname.lastname@example.org