Creating a Duplex WCF Service

Duplex Service

What is Duplex Service?

Duplex service contract is a message exchange pattern in which both endpoints can send messages to others independently. Duplex communication occurs when a client connects to a service and provides the service with a channel on which the service can send message back to the client. BasicHttpBinding and wsHttpBinding does not support duplex communication but wsDualHttpBinding and netTcpBinding does support.

How to create a duplex service?

In order to create a duplex service you create a pair of interfaces. One interface is a Service Contract which defines the operations which a client can invoke, while other is a callback. The service contract must specify a callback contract property in its ServiceContract attribute.

– Duplex pattern can be implemented using request/response operation

– Duplex pattern can be implemented using one-way operation

Why use duplex service

Consider a service which has a method processReport. The method generates a report and takes 5 to 10 minutes to complete generation of report. If we use request/response pattern then the client is blocked until the report is generated. To free up the client we use one-way operation. But if we use one way pattern the client cannot be notified after the report is generated or if the report generation fails due to some reason. Here duplex services comes into picture.

How to create callbacks

We need to create an interface and associate it with the Service contract. Only one callback can be associated with a given service contract. To associate a service with call back use the Callback Property of the ServiceContract attribute.

InstanceContext class

We require instance context class at the client to create an instance of the class and pass it to the constructor of the proxy class


We must use a binding that supports duplex communication for WCF service.

Code Sample

Step I: Create Two interfaces

    [ServiceContract(CallbackContract = typeof(IProcessDataCallback))]
    public interface IProcessData
        [OperationContract(IsOneWay = true)]
        void GenerateReport();

    public interface IProcessDataCallback
        [OperationContract(IsOneWay = true)]
        void PercentComplete(int percent);


Step II: Implement the Service contract at server side

public void GenerateReport()
    for (int i = 1; i <= 100; i++)

Step III: Modify the config file as follows

  <service name="DuplexService.ProcessData" behaviorConfiguration="myBehavior">
    <endpoint address="" binding="wsDualHttpBinding" contract="DuplexService.IProcessData"></endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
    <behavior name="myBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>

Step IV: Deploy the service and start building the client

Step V: Add service Reference to the client and implemente the client

private void button1_Click(object sender, EventArgs e)
	InstanceContext ic = new InstanceContext(this);
	ProcessData.ProcessDataClient client = new ProcessData.ProcessDataClient(ic);

Step VI: Implement the callback

public void PercentComplete(int percent)


label1.Text = percent + “% Complete”;



One thought on “Creating a Duplex WCF Service

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s