The article explains on how to implement Duplex contract in WCF.
In our earlier sessions we have discussed about the how to create a simple WCF and test that. Today in this article we will see in details about the Duplex contract and how to implement in our WCF service.
What is a duplex contract?
A contract that allows clients and servers to communicate with each other independently so that either can initiate calls to the other is called as Duplex Contract. The duplex contract is one of three message patterns available to Windows Communication Foundation (WCF) services, other two being One Way Contract and Request-Reply Contract.
We use this kind of contract when our service query the client for some additional tasks or more information or explicitly raise events on the client.
Let’s start to build our WCF service demonstrating Duplex Contract
Create the interface that makes up the server side of the duplex contract and declare the method signatures. And also create the callback interface that defines the set of operations that the service can invoke on the client.
Here we have declared one method GetDataFromXml to retrieve data from xml file and one callback method Filterdata that will be defined on the client side.
Now let’s define Service1 class which implements a duplex service contract. And also add an InstanceContextMode of PerSession to store the result. So that an instance of the service will be bound to each duplex session.
Link the two interfaces into a duplex contract by setting the CallbackContract property in the primary interface to the type of the callback interface.
Lets define the sample data as below. You can also get data from database also.
Lets configure the web.config file, Creating the endpoint. For duplex contract we need to set the binding as wsDualHttpBinding as below.
Build and run the service we will see this
Now let’s build our client application
Now Use the ServiceModel Metadata Utility Tool (Svcutil.exe) to generate contracts (interfaces) for the client. And add that service proxy client and output.config file to the client application and all rename it to App.config file.
Add a console application and Implement the callback interface in the client class, as shown below.
The implementation call back method just filters the data from the result obtained from the WCF service and the records were printed on the screen.
Set the endPointAddress and create an instance of the InstanceContext class. The constructor requires an instance of the client class.
Create an instance of the WCF client using the constructor that requires an InstanceContext object. The second parameter of the constructor is the name of an endpoint found in the configuration file.
Then call the GetDataFromXml method using the client object.
Build and Run the client Application.
If you observer clearly the Client application calls the WCF service which returns the Dataset containing all the records and it initiates the call back method which filters the data and displays the result.
So we saw how the client and server communicate with each other independently using the Duplex Contract.
Hope you all like this article.