Message Exchange Pattern

Message Exchange Pattern (MEP) specifies request message and response message handling in a service operation.  Three are three possibilities.

  • Request Reply – client expects response returned in an operation.
  • One-Way – client does not expect response returned in an operation.
  • Duplex – client expects response in an operation. But the response is returned by a separated process.

Request Reply and One Way

Declaring Request Reply and One-Way MEP is simple.  OperationContractAttribute.IsOneWay property specifies the intent.

 1 namespace MepLibrary
 2 {
 3     using System.ServiceModel;
 4 
 5     [ServiceContract]
 6     public interface IMyService
 7     {
 8         /* Note 1 */[OperationContract]
 9         string Default_Request_Reply_Mep();
10 
11         [OperationContract(/* Note 2 */ IsOneWay = false)]
12         string Explicit_Request_Reply_Mep();
13 
14         [OperationContract(/* Note 3 */ IsOneWay = false)]
15         void Void_Request_Reply_Mep();
16 
17         [OperationContract(/* Note 4 */ IsOneWay = true)]
18         void One_Way_Mep();
19 
20         /* Note 5 */
21         //[OperationContract(IsOneWay = true)]
22         //string Illegal_One_Way_Mep();
23         //void Illegal_One_Way_Mep(out string input);
24         //void Illegal_One_Way_Mep(ref string input);
25     }
26 }
  • Note 1.  Default service operation uses request response MEP.
  • Note 2.  Specifying IsOneWay = false declares request reply MEP.
  • Note 3.  A void method with request reply MEP returns an empty response.
  • Note 4.  Specifying IsOneWay = true declares one way MEP.  A service operation does not return response message.
  • Note 5.  Specifying one way MEP with return value, ref, or out parameter are illegal.  The code can compile, but causes runtime exception.

Duplex

Implementing duplex MEP is more involving.

 1 namespace MepLibrary
 2 {
 3     using System.ServiceModel;
 4 
 5     [ServiceContract(/* Note 1 */ CallbackContract = typeof(IDuplexCallback),
 6                      /* Note 3 */ SessionMode = SessionMode.Required)]
 7     /* Note 2 */ interface IDuplexService
 8     {
 9         [OperationContract(IsOneWay = true)]
10         void Operation();
11     }
12 
13     interface IDuplexCallback
14     {
15         [OperationContract(IsOneWay = true)]
16         void Response(string input);
17     }
18 
19     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
20     class MyDuplexService : IDuplexService
21     {
22         public void Operation()
23         {
24             /* Note 4 */
25             OperationContext.Current
26                             .GetCallbackChannel<IDuplexCallback>()
27                             .Response("input");
28         }
29     }
30 }
  • Note 1.  Callback contract interface serves as response contract from server back to client.
  • Note 2.  Service contract, like other MEP, serves as operation contract.
  • Note 3.  Session serves to establish context between server and client.
  • Note 4.  This line of code returns response message back to client explicitly.

Further Study