Blog

Filter posts by Category Or Tag of the Blog section!

Duplex Message Exchange Pattern in WCF

Sunday, 25 August 2013

There are 3 message exchange patterns (MEP) in the WCF: Request/Response, One Way and Duplex - I'm going to talk about the 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 a good idea that the client waits until receiving the response, so we can use Duplex pattern to solve this. It will provide the client to get the response right after completing the request operation.  In other words, the 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:

 

   public interface IDuplexResponse
    {
        string DuplexMethod(string parameter);
    }

    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IDuplexResponse))]
    public interface IDuplexService
    {
        [OperationContract]
        string ReturnSomeValue(string value);
    }

 

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

 

    [ServiceContract(SessionMode = SessionMode.Required,
                 CallbackContract = typeof(IAccountServiceCallback))]
    public interface IAccountService
    {
        [OperationContract(IsOneWay = true)]
        void AddTo(double amount);
     
        [OperationContract(IsOneWay = true)]
        void Tax(double amount);
    }

 

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

 

    public interface IAccountServiceCallback
    {
        [OperationContract(IsOneWay = true)]
        void Equals(double result);
    }

 

Now let's implement the AccountService class

 

    public class AccountService :IAccountService
    {
        private IAccountServiceCallback _accountServiceCallback = null;

        public AccountService()
        {
            _accountServiceCallback = OperationContext.Current.GetCallbackChannel<IAccountServiceCallback>();
        }


        public void AddTo(double amount)
        {
            throw new NotImplementedException();
        }

        public void Tax(double amount)
        {
            throw new NotImplementedException();
        }
    }

 

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 a response to the client.

 

Category: Software

Tags: WCF

comments powered by Disqus