Ehsan Ghanbari

Experience, DotNet, Solutions

Duplex Message Exchange Pattern in WCF

There are 3 message exchange patterns (MEP) in the WCF: Request/Response , One Way and Duplex - I'm going to talk about Duplex pattern in this post. In duplex pattern Both the client and the server can send messages to each other independently. This can be used by request/Response messaging pattern too but In the request/Response and one-way message exchange patterns, the communication is initiated by the client application, but in the duplex message exchange pattern, both the client and the service can initiate the communication. If a client calls a long running method on a service, it's not good idea that the client wait until receiving the response, so we can use Duplex pattern to solve this. It will provide client to get the response right after completing the request operation.  In other words duplex pattern is consists of two one way pattern. You can use this pattern when you want the service to send a message or alert to the client after the client has called the service.


Take a look at this piece of code, all you need to do to achieve this pattern

  1.    public interface IDuplexResponse
  2.     {
  3.         string DuplexMethod(string parameter);
  4.     }
  6.     [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IDuplexResponse))]
  7.     public interface IDuplexService
  8.     {
  9.         [OperationContract]
  10.         string ReturnSomeValue(string value);
  11.     }



Let's consider Another example, this is a little bit closer to real world applications, create the IAccountService interface with this definition

  1.     [ServiceContract(SessionMode = SessionMode.Required,
  2.                  CallbackContract = typeof(IAccountServiceCallback))]
  3.     public interface IAccountService
  4.     {
  5.         [OperationContract(IsOneWay = true)]
  6.         void AddTo(double amount);
  8.         [OperationContract(IsOneWay = true)]
  9.         void Tax(double amount);
  10.     }


And Create the IAccountServiceCallback interface like this(duplex or two way)

  1.     public interface IAccountServiceCallback
  2.     {
  3.         [OperationContract(IsOneWay = true)]
  4.         void Equals(double result);
  5.     }


Now let's implement the AccountService class

  1.     public class AccountService :IAccountService
  2.     {
  3.         private IAccountServiceCallback _accountServiceCallback = null;
  5.         public AccountService()
  6.         {
  7.             _accountServiceCallback = OperationContext.Current.GetCallbackChannel<IAccountServiceCallback>();
  8.         }
  11.         public void AddTo(double amount)
  12.         {
  13.             throw new NotImplementedException();
  14.         }
  16.         public void Tax(double amount)
  17.         {
  18.             throw new NotImplementedException();
  19.         }
  20.     }


Got it!?

ServiceContract and OperationContract attributes allow you to generate the service contract and also it's worth to say that The difference between Request-response and Duplex pattern is that Request-response pattern requires the response on same communication channel whereas Duplex creates the separate communication channel to return response to client.


About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In MVC
Multiple submit buttons in MVC
Domain driven design VS model driven architecture
What's the DDD-lite?