Can you explain duplex contracts in WCF ?
Can you Explain Duplex Contracts in WCF
Hi
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nattudurai EswaramurthyRank #5,536 · 296 reputationPosted Aug 16, 2012, 3:04 AM EDT
To create a duplex contract
Create the interface that makes up the server side of the duplex contract.
Apply the ServiceContractAttribute class to the interface.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode=SessionMode.Required, CallbackContract=typeof(ICalculatorDuplexCallback))] public interface ICalculatorDuplex { [OperationContract(IsOneWay=true)] void Clear(); [OperationContract(IsOneWay = true)] void AddTo(double n); [OperationContract(IsOneWay = true)] void SubtractFrom(double n); [OperationContract(IsOneWay = true)] void MultiplyBy(double n); [OperationContract(IsOneWay = true)] void DivideBy(double n); }Declare the method signatures in the interface.
Apply the OperationContractAttribute class to each method signature that must be part of the public contract.
Create the callback interface that defines the set of operations that the service can invoke on the client.
public interface ICalculatorDuplexCallback { [OperationContract(IsOneWay = true)] void Equals(double result); [OperationContract(IsOneWay = true)] void Equation(string eqn); }Declare the method signatures in the callback interface.
Apply the OperationContractAttribute class to each method signature that must be part of the public contract.
Link the two interfaces into a duplex contract by setting the CallbackContract property in the primary interface to the type of the callback interface.
To call methods on the client
In the service's implementation of the primary contract, declare a variable for the callback interface.
Set the variable to the object reference returned by the GetCallbackChannel method of the OperationContext class.
Call the methods defined by the callback interface.
Example
The following code example demonstrates duplex communication. The service's contract contains service operations for moving forward and backward. The client's contract contains a service operation for reporting its position.
// Define a duplex service contract. // A duplex contract consists of two interfaces. // The primary interface is used to send messages from client to service. // The callback interface is used to send messages from service back to client. // ICalculatorDuplex allows one to perform multiple operations on a running result. // The result is sent back after each operation on the ICalculatorCallback interface. [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode=SessionMode.Required, CallbackContract=typeof(ICalculatorDuplexCallback))] public interface ICalculatorDuplex { [OperationContract(IsOneWay=true)] void Clear(); [OperationContract(IsOneWay = true)] void AddTo(double n); [OperationContract(IsOneWay = true)] void SubtractFrom(double n); [OperationContract(IsOneWay = true)] void MultiplyBy(double n); [OperationContract(IsOneWay = true)] void DivideBy(double n); } // The callback interface is used to send messages from service back to client. // The Equals operation will return the current result after each operation. // The Equation opertion will return the complete equation after Clear() is called. public interface ICalculatorDuplexCallback { [OperationContract(IsOneWay = true)] void Equals(double result); [OperationContract(IsOneWay = true)] void Equation(string eqn); } // Service class which implements a duplex service contract. // Use an InstanceContextMode of PerSession to store the result // An instance of the service will be bound to each duplex session [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class CalculatorService : ICalculatorDuplex { double result; string equation; ICalculatorDuplexCallback callback = null; public CalculatorService() { result = 0.0D; equation = result.ToString(); callback = OperationContext.Current.GetCallbackChannel(); } public void Clear() { callback.Equation(equation + " = " + result.ToString()); result = 0.0D; equation = result.ToString(); } public void AddTo(double n) { result += n; equation += " + " + n.ToString(); callback.Equals(result); } public void SubtractFrom(double n) { result -= n; equation += " - " + n.ToString(); callback.Equals(result); } public void MultiplyBy(double n) { result *= n; equation += " * " + n.ToString(); callback.Equals(result); } public void DivideBy(double n) { result /= n; equation += " / " + n.ToString(); callback.Equals(result); } }Thanks,
If this article helps to understand Duplex Contract, plz select "Accepted Answer"
Nattudurai EswaramurthyRank #5,536 · 296 reputationPosted Aug 16, 2012, 3:05 AM EDT
Figure 18:- Duplex Service code
Let us try to understand the duplex concept by doing a small sample. The code snippet is as shown in the above figure. We will extend the previous sample, which was shown in the first question only that now we will provide the notification to the client once the doHugeTask is completed.
The first step is to change the service class. Above is the code snippet of the service class. Again, we have numbered them so that we can understand how to implement it practically line by line. So below is the explanation number by number:-
1 - In the service contract attribute we need to specify the callback contract attribute. This Callback Contract attribute will have the interface, which will define the callback.
2 - This is the interface which client will call. In order that it should be asynchronous, we have defined the one-way attribute on the doHugeTask method.
3 -This is the most important interface. As it forms the bridge of communication between server and client. The server will call the client using Completed method. Client needs to provide implementation for the completed method at the client side and server will call that method once it completes the doHugeTask method.
4 and 5 - In this we implement the Interface Duplex interface and provide implementation for doHugeTask () method.
6 - This is an important step. The OperationContext.Current.GetCallBackChannel will be used to make callback to the client.
7 - We will expose the service using HTTP protocol. In this sample, because we want to do duplex communication we need to use wsDualHttpBinding rather than just using simple Http Binding. This new binding configuration is then assigned to the end on which the service is hosted.
This completes the server side explanation for duplex.
Figure: - 19 Duplex Client side code
Above is the code snippet for client side. So let us explain the above code with numbers.
1- We implement the completed method. This is the callback method, which the server will call when doHugeTask is completed.
2- In this step we create object of the Instance Context class. Instance Context represents context information of a service. The main use of Instance Context is to handle incoming messages. In short, proxy is used to send messages to server and Instance Context is used to accept incoming messages.
3 -In this step we pass the Instance Context object to the constructor of the proxy. This is needed, as the server will use the same channel to communicate to the client.
4 - In this section two windows are shown. The top windows is the servers output and the below windows is of the client. You can see in the below window that server as made a callback to the client.
Note: - You can get source code for the same in WCFDuplex folder. Feel Free to experiment with it. Try making one simple project of client to client chat using WCF duplex fundamentals I am sure your doubts will be cleared in and out.