Message exchange patterns in WCF
In this article I am going to explain about MEP in WCF.
There are three Message exchange patterns in SOA
1. Request / Response MEP
2. One Way MEP
3. Duplex MEP
The message patterns are very important for interview about WCF questions
Below are the message exchange patterns in SOA.
1. Request / Response MEP:
This is the most used mep as many real time operation required some status or data back in response to request. To use this mep you do not need to do anything as the default value of IsOneWay property for OperationContract is false. So your OperationContract is enabled for request and response. If you are using void return type of OperationContract and using Request / Response then response message is still going back to client with empty soap body. In following code sample operation contract are mark as request / response.
[ServiceContractAttribute]
public interface IMyContract
{
[OperationContractAttribute]
public void GetData(int ID);
}
2. One Way MEP:
Some time service clients just need to execute the operation without expecting any response from service just to trigger some business logic like marking product as disable in database. For marking OperationContract as OneWay mep you need to set OperationContract attribute IsOneWay to true. One Way mep is very good choice if your using any queue or using MSMQ bindings. You cannot use the faultcontracts or transction contexts with OneWay MEP.
[ServiceContractAttribute]
public interface IMyContract
{
[OperationContractAttribute(IsOneWay=true)]
public void GetData(int ID);
}
3. Duplex MEP:
The duplex MEP is two way message channel where client and service both can execute the contracts on other side. For using duplex mep we need to use two contracts one service contract and call back contract. The callback contract will be associated with service contract. For an example the GetData service client sends one way message to service and after some operations at service level , service calls back client and execute call back operation. The Duplex MEP does not work with real word scenario where service requires active connection with client which can be dangerous in terms of security. It also requires long running sessions.
public interface IProductsDuplexCallback
{
[OperationContract(IsOneWay = true)]
void ShowProduct(Product product);
}
[ServiceContract(CallbackContract=typeof(IProductsDuplexCallback))]
public interface ICalculatorDuplex
{
[OperationContract(IsOneWay = true)]
void GetProduct(int ID);
}