In Windows Communication Foundation, both parties (i.e. a service and client) communicate with each other and exchange messages using a certain pattern known as Message Exchange Pattern (MEP). WCF supports three different types of MEPs (Message Exchange Patterns).
- Request-Reply
- One-Way
- Duplex
In this WCF Tutorial Series, we will explore all three Message Exchange Patterns step by step with the help of sample code.
Request-Reply Message Exchange Pattern:
Windows Communication Foundation uses Request-Reply as default pattern for communication. In this pattern, when a service operation is called, it always generate a response message to caller (i.e. a client). The client will receive a response even if the service operation has void return type (receiving an empty SOAP body in this case).
A client always waits for a reply from service for certain time (i.e. ReceiveTimeout) and then execute the following piece of code. Default value for ReceiveTimeout is one minute, after that it throws an exception of type TimeoutException.
Almost all WCF Bindings support Request-Reply MEP except the followings:
- NetMsmqBinding
- NetPeerTcpBinding
Because Request-Reply is the default Message Exchange Pattern in WCF, so we don’t need to do anything to enable it. But for the purpose of understanding, we can explicitly set it enabled as follows:
public interface RequestReplyMEPService
{
[OperationContract(IsOneWay = false)]
void SaveData(string value);
}
One-Way Message Exchange Pattern:
In certain scenarios, when we are not interested to receive response message back, instead just wanted to execute a certain business functionality by sending a message to WCF service. For example, we wanted to execute a long running business process or want queued message delivery. WCF provides One-Way Message Exchange Pattern (MEP) to fulfill such scenarios.
In One-Way MEP, client doesn’t need to wait for response after sending message to WCF service. Even if some error occurs in communication, WCF Service doesn’t send any response back. That’s why One-Way Message Exchange Pattern provides no support for followings in service operations:
- no output parameters
- no reference parameters
- no return value
All WCF bindings support One-Way Message Exchange Pattern. In order to enable One-Way Message Exchange Pattern in WCF, do the follows:
public interface OneWayMEPService
{
[OperationContract(IsOneWay = true)]
void SaveData(string value);
}
Duplex Message Exchange Pattern:
Duplex “as it’s name indicates” is a two-way message channel. In certain scenarios, we want to get a confirmation notification back from WCF service as an operation completes at service end. In more simple words, we can say that both service as well as client can send messages to eachother when using Duplex MEP. So, this message sending in duplex pattern can be request-reply (synchronous) or one-way (asynchronous).
Below diagrams further enhances the concept of Duplex MEP.
In order to implement Duplex pattern for message exchange, CallbackContract is additionally required along with ServiceContract. We need to associate CallbackContract with ServiceContract through CallbackContract property of our ServiceContract.
For example, we have a CallbackContract named “DuplexMEPServiceCallback” as follows:
public interface DuplexMEPServiceCallback
{
[OperationContract(IsOneWay = true)]
void ProcessComplete(int value);
}
So, we will associate above CallbackContract to following ServiceContract named “DuplexMEPService” through CallbackContract property as follows:
public interface DuplexMEPService
{
[OperationContract(IsOneWay = true)]
void CallProcess(string value);
}
Duplex MEP being a two-way message channel works with few bindings that has bi-directional capabilities like wsDualHttpBinding. So, endpoint will be defined
as follows:
There are always some concerns while using Duplex MEP that must be considered or addressed as:
- Maintaining a long-running session between parties can create scalability problems.
- It requires a connection back to client which is always not possible.
We have discussed in details about all three Message Exchange Pattern in this WCF Tutorial with respective scenarios. Your feedback is really important for me. You can leave your feedback about how fruitful the content is? Enjoy WCF Service Programming.
To test and further enhance your WCF skills
Other WCF and related Tutorials:
- WCF Hosting (Console | Windows Service | IIS | WAS )
- Your First WCF RESTful Service – Step by Step
- WCF Vs ASMX Service
- Difference betweeen ASP.NET WebForms and ASP.NET MVC
- Post JSON data to WCF RESTful Service using jQuery
- Your First ASP.NET Web API Service
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions