How to achieve Duplex Communication in WCF using Duplex Contract in C#

 Most of us are aware of how to use WCF to call server side API from client, but there is also a way to call client side API's from the Server. This is possible with the use of Duplex contract in WCF. With the help of duplex contract we will be able to achieve duplex communication. In this post we will be seeing how to accomplish this with the help of an example.

How to Define Duplex Contract in WCF?

To define the capabilities of a service we define what is called as a Service Contract which is also used for creating the proxies at the client side. While defining the Service contract there is a parameter called CallbackContract which takes in a type of Interface which is used to define the client side API's.

Service Contract for WCF

In the above example we have defined a Service Contract with a Callback Contract of type IStateChangeCallback which is an interface which exposes the API's which can be defined at the client side. In a normal WCF service we would not define the Callback contract.

Server Side Implementation

Now coming to the implementation of the Service we will refer an instance of the Callback Contract implementation which can be later used to call the client side API. Below figure demonstrates how this can be done.

WCF Service Implementation with callback contract

In the constructor of the service we are querying the OperationContext to get an instance of the call back contract instance.

Here what we are trying to do is the client will call the GetData API which will start the timer and once the timer is expired we have registered an event handler called Timer_Elapsed which uses the call back contract instance to call the client side API called OnStateChanged.

Client Side Implementation


Client side implementation of callback

Before writing the Client Side implementation as shown above we have to add the Service Reference of the WCF service by clicking on Add Service Reference.

Once its added then in the client side we have defined the class StateCallBackHandler which implements the callback contract which is generated at the client side by referring the WCF Service.

In the Main method we have created the instance of InstanceContext and passed the instance of the Callback Handler as an argument. 

Then we have to create an instance of the client passing the instance context and call the client side API GetData(). 

After executing the above code we will get the output as shown below:

Output

The first line is the data returned from the server and the second line is printed from the client side after a delay of 10 seconds as the Timer interval at the server was set to 10 seconds.

One point to note here is the binding which should be used for the Duplex Contract WCF Service, normal basichttpBinding will not support Duplex Contract.

we have to use Dual variant of the binding protocol such as "wsDualHttpBinding" to support Duplex Contract.


Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples