3.3 Example 3 (WCF): Reliable Messaging with TCP as Transport

This example illustrates how reliable messaging can be used in Windows Communication Foundation (WCF). This example describes the main scenario of the Use a Web Service with Reliable Messaging use case.

The code example shows that the following interface is used to establish the service contract.

 [ServiceContract]
 public interface IMessage
 {
     [OperationContract]
     void Message1();
     [OperationContract]
     void Message2();
 }

The user is required to add the following configuration entries to enable reliable messaging over TCP.

 <system.serviceModel>
    <services>
       <service name="<ServiceName>" >
          <host>
             <baseAddresses>
                <add baseAddress="net.tcp://<ServerName>:<Port>/service"/>
             </baseAddresses>
          </host>
          <endpoint address=""
                    binding="customBinding"
                    bindingConfiguration="TcpBinding"
                    bindingName="<TcpBinding_Name>"
                    contract="IMessage" />
       </service>
    </services>
  
    <bindings>
       <customBinding>
          <!-- Configure a CustomBinding that supports tcp transport
               and text encoding -->
          <binding name="TcpBinding">
          <!--  This will enable the WS Reliable messaging  -->
             <reliableSession flowControlEnabled ="false"/>
             <textMessageEncoding messageVersion="Soap12WSAddressing10"/>
             <tcpTransport/>
          </binding>
       </customBinding>
    </bindings>
 </system.serviceModel>
  

The user sends two messages and finally a third message with the <LastMessage> element tag.

The .NET Message Framing Protocol ([MC-NMF]) is used to frame the SOAP messages over TCP. This example focuses on SOAP messages, which are sent by using [MC-NMF] messages to frame them, as follows:

The Initiator and Receiver exchange a set of Preamble messages, as described in [MC-NMF] sections 3.2.4.2 and 3.3.4.2. After a session is established by using Preamble messages, the Initiator and Receiver send and receive SOAP messages, as described in [MC-NMF] sections 3.2.4.3, 3.2.4.4, 3.3.4.3, and 3.3.4.4. After the message exchange is complete, the Initiator and Receiver close the session by sending an End Record message, as described in [MC-NMF] section 3.2.4.5 and 3.3.4.5.