Message Exchange Patterns in WCF

In WCF, The Message Exchange Pattern (MEP) provides a way of communication between client and server. MEP is a beauty of WCF. A services do some task depend on us or send a response to our request. The communication between client and server are done in form of messages. When we send a message as request and get a message as response from the service. The WCF Services support three types of Message Exchange Pattern:

  • Request-Response

  • One-Way

  • Duplex

Request-Response

This is the default message exchange pattern. In this MET communication, client sends the request message to the service and wait for the response message from the service. The default receive timeout period is one minute. If the service does not responded to the client then the client receives a TimeoutException after one minute. In this MET, the client expect a response message even if the the service operation's return type is void. The Request-Response pattern support all the bindings except MSMQ based bindings.

[ServiceContract]

    1. public interface IMyService  
    2.   
    3. {    
    4.       [OperationContract]  
    5.   
    6.       string GetName( string name);  
    7.   
    8.       [OperationContract(IsOneWay = false)]  
    9.   
    10.       void AddName(string name);    
    11. }  

By default the IsOneWay property is false but for this communication, we need to set IsOneWay property to false explicitly. This communication's operation returns a header with status code 200(OK) and a full SOAP response in the message body.

One-Way

In this MET communication, The client sends request message to the service and does not wait for response message from service. There is one disadvantage of this pattern, the service does not sends back any response message even if any communication error occurs. So, we should use this MET when the service does some operation but does not reply a response. For example, if we want to change the record active status from Inactive to Active and we do not want to get a confirmation message from the service that the record status is changed. We can not use FaultContract with this pattern because FaultContract uses two-way channel. All the WCF bindings support this pattern.

           [ServiceContract]

    1. public interface IMyService  
    2.   
    3. {  
    4.   
    5.       [OperationContract(IsOneWay = true)]  
    6.   
    7.       void ChangeRecordStatus(string value);  
    8.   
    9. }  

In One-Way communication, we set IsOneWay property of OperationContract attribute to true. This pattern returns only header with the status code 202 (Accepted) and does not return message body.

Duplex

In Duplex MET, the client and services can communicate to each other with using one-way or request-response messages. This pattern use two types of contract, ServiceContract and CallbackContract for its implementation. We can use FaultContract with this pattern because FaultContract uses two-way channel. The all bidirectional capable bindings support duplex pattern.

         [ServiceContract(CallbackContract = typeof(DuplexServiceCallback))]

    1. public interface IMyService  
    2.   
    3. {  
    4.   
    5.       [OperationContract(IsOneWay = true)] //one-way message  
    6.   
    7.       void AddName(string name);  
    8.   
    9.       [OperationContract] //request-response message  
    10.   
    11.       string GetName( string name);  
    12.   
    13. }  

We must specified the binding as wsDualHttpBinding in web.config for Duplex pattern. This MET is problematic because it requires a connection to send back to the client and there may be a change that not connect back the client due to firewall and network address translation problems.

Next Recommended Reading Error Message Logging using WCF