Duplex Contract in WCF and how to implement it?

Madhu.b.rokkam
Posted by in WCF category on for Intermediate level | Points: 250 | Views : 33289 red flag

The article explains on how to implement Duplex contract in WCF.


 Download source code for Duplex Contract in WCF and how to implement it?

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.

Conclusion


So we saw how the client and server communicate with each other independently using the Duplex Contract.

Hope you all like this article.

Page copy protected against web site content infringement by Copyscape

About the Author

Madhu.b.rokkam
Full Name: Madhu Rokkam
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/13/2011 3:13:20 PM
Country: India
Thanks and Regards Madhu
http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Tripati_tutu on: 2/21/2011 | Points: 25
This article is helpful....
Posted by: Madhu.b.rokkam on: 2/21/2011 | Points: 25
Thanks Tripati..
Posted by: Nikhil_Soni on: 7/19/2012 | Points: 25
please let me know how it's hosted in network computer and internet.?

Login to post response

Comment using Facebook(Author doesn't get notification)