Overview of Message Exchange Patterns (MEP) in WCF

What is WCF?
WCF is a Microsoft platform for building  distributed and interoperable applications and it keeps data in XML and JSON format that can be understood by any language. WCF service can communicate in XML and JSON message formats with the client.





Message Exchange Pattern in WCF
We know that WCF keeps data in XML or JSON format. When a client sends a request to a service and the service sends a response to the client then the request and response is in XML or JSON format, in other words the message exchange is in the format of XML or JSON between the client and the service.

Message Exchange Patterns describes the way of communication between Client and Server means how client and server would be exchange messages to each other. There are three types of message exchange patterns

Uses of the Message Exchange Pattern:
  • When we want the calling of the function to not take more time
  • We just want to call the function and not want wait for the response.
  • When we want that function call is not blocking and at the same time we want something out of the function (response) for our application.
  • When we want a two-way communication mechanism.
Types of Message Exchange Pattern:
1- Request/Response
2- One way
3- Duplex

1. Request / Response
In this communication, client sends the message to the service and waits for reply from the service. Within a ReceiveTimeout period (default timeout is one minute), if the service doesn't respond to the client then the client will receive a TimeoutException. In this pattern, client waits for the reply mes-sage, even if the service operation's return type is void.

This the by default message exchange pattern in WCF. All WCF bindings except MSMQ-based bindings supports this pattern.

[ServiceContract]
public interface RequestReplyService
{ 
 [OperationContract]
 string GetData(int value);
 
 [OperationContract(IsOneWay = false)]
 void SaveData(string value);
}

2- One-Way
To define this pattern, you can set IsOneWay property to false explicitly, but by default it is false. So there is need to define ISOneWay property for Request-Reply pattern. A Request-Reply operation returns header with an HTTP status code of 200 (OK) and a full SOAP response in the message body.


This pattern doesn’t supports output parameters, by-reference parameters and return value to an operation, otherwise you will receive an InvalidOperationException.
All of the WCF bindings support one-way operations.


[ServiceContract]
public interface OneWayService
{
 [OperationContract(IsOneWay = true)]
 void SaveData(string value);
}

A One-Way operation returns only header with an HTTP status code of 202 (Accepted) without message body. This pattern is commonly used with per-call or singleton services only.

3- Duplex
In this communication, client and services can sends messages to each other by using One-way or request-reply messaging

Duplex(Request-Reply)

Duplex(One-Way)



Only bidirectional-capable bindings support this pattern like as WS Dual, TCP and IPC bindings.
To make a duplex contract, you must also define a callback contract and assign the typeof that callback con-tract to the CallbackContract property of your service contract’s ServiceContract attribute as shown in be-low example.


[ServiceContract(CallbackContract = typeof(DuplexServiceCallback))]
public interface DuplexService
{
 [OperationContract(IsOneWay = true)] //One-Way
 void SaveData();
 
 [OperationContract] //Request-Reply.
 string GetData();
 }
 
 public interface DuplexServiceCallback
 {
 [OperationContract(IsOneWay = true)]
 void Progress(string status);
 }

For this pattern, you must also specified the binding as wsDualHttpBinding with in your web.config as shown below-

<services>
 <service name="WCFServiceApp.DuplexService">
 <endpoint address ="" binding="wsDualHttpBinding"
  con-tract="WCFServiceApp.IDuplexService">
 </endpoint>
 </service>
</services>


Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment