Skip to main content

Understanding Message Exchange Patterns (MEP) in WCF

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.


  • Request-Reply

    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.
    1. [ServiceContract]
    2. public interface RequestReplyService
    3. {
    4. [OperationContract]
    5. string GetData(int value);
    6.  
    7. [OperationContract(IsOneWay = false)]
    8. void SaveData(string value);
    9. }
    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.
  • One-Way

    In this communication, client sends the message to the service and doesn't wait for reply from the service. In this pattern, receiver doesn’t send any response to the sender, even if any error occurs in the communication.
    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.
    1. [ServiceContract]
    2. public interface OneWayService
    3. {
    4. [OperationContract(IsOneWay = true)]
    5. void SaveData(string value);
    6. }
    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.
  • Duplex

    In this communication, client and services can sends messages to each other by using One-way or request-reply messaging.
    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.
    1. [ServiceContract(CallbackContract = typeof(DuplexServiceCallback))]
    2. public interface DuplexService
    3. {
    4. [OperationContract(IsOneWay = true)] //One-Way
    5. void SaveData();
    6.  
    7. [OperationContract] //Request-Reply.
    8. string GetData();
    9. }
    10.  
    11. public interface DuplexServiceCallback
    12. {
    13. [OperationContract(IsOneWay = true)]
    14. void Progress(string status);
    15. }
    For this pattern, you must also specified the binding as wsDualHttpBinding with in your web.config as shown below-
    1. <services>
    2. <service name="WCFServiceApp.DuplexService">
    3. <endpoint address ="" binding="wsDualHttpBinding" con-tract="WCFServiceApp.IDuplexService">
    4. </endpoint>
    5. </service>
    6. </services>
  • Comments

    Popular posts from this blog

    gcAllowVeryLargeObjects Element

    There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {          Re=re;          Im=im;      } } As we know this structure consumes about 16

    Support for debugging lambda expressions with Visual Studio 2015

    Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitations.

    How to allow a very large object in .net application?

    Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio, and create C# Console Application. Lets create simple struct with two double members like example below: public struct ComplexNumber { public double Re; public double Im; public ComplexNumber(double re, double im) { Re = re; Im = im; } } As we know this structure consumes about 16 bytes of memory. So if we want to create array of this type which consume more than 2GB we need to create array at least with 134217728 instances. So this sample program below creates 130000000 (about 1,97 GB) of array. int maxCount = 130000000; ComplexNumber[] arr = null; try { arr = new ComplexNumber[maxCount]; } catch (Exception ex) { Console.WriteLine(ex.Message); } So if we run t