Custom Binding Reliable Session

A custom binding is defined by an ordered list of discrete binding elements. This sample demonstrates how to configure a custom binding with various transport and message encoding elements, especially enabling reliable sessions.


The samples may already be installed on your machine. Check for the following (default) directory before continuing.


If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.


Sample Details

Reliable sessions provide features for reliable messaging and sessions. Reliable messaging retries communication on failure and allows delivery assurances such as in-order arrival of messages to be specified. Sessions maintain state for clients between calls. The sample implements sessions for maintaining client state and specifies in-order delivery assurances. The sample is based on the Getting Started that implements a calculator service. The reliable session features are enabled and configured in the application configuration files for the client and service.


The set-up procedure and build instructions for this sample are located at the end of this topic.

The ordering of binding elements is important in defining a custom binding, because each represents a layer in the channel stack (see Custom Bindings).

The service configuration for the sample is defined as shown in the following code example.

<?xml version="1.0" encoding="utf-8" ?>  
        <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc  -->  
        <!-- specify customBinding binding and a binding configuration to use -->  
        <endpoint address=""  
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />  
        <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->  
        <endpoint address="mex"  
                  contract="IMetadataExchange" />  

    <!-- custom binding configuration - configures HTTP transport, reliable sessions -->  
        <binding name="Binding1">  
          <reliableSession />  
          <security authenticationMode="SecureConversation"  
          <compositeDuplex />  
          <oneWay />  
          <textMessageEncoding messageVersion="Soap12WSAddressing10" writeEncoding="utf-8" />  
          <httpTransport authenticationScheme="Anonymous" bypassProxyOnLocal="false"  
                        proxyAuthenticationScheme="Anonymous" realm=""   
                        useDefaultWebProxy="true" />  

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->  
        <behavior name="CalculatorServiceBehavior">  
          <serviceMetadata httpGetEnabled="True"/>  
          <serviceDebug includeExceptionDetailInFaults="False" />  



When running in a cross-machine scenario, you must change client's endpoint address to reflect the host name of the service.

When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client.

Add(100,15.99) = 115.99  
Subtract(145,76.54) = 68.46  
Multiply(9,81.25) = 731.25  
Divide(22,7) = 3.14285714285714  

Press <ENTER> to terminate client.  

To set up, build, and run the sample

  1. Install ASP.NET 4.0 using the following command:

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable  
  2. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  3. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  4. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.


    When running the client in a cross-machine configuration, be sure to replace "localhost" in both the address attribute of the <endpoint> element and the clientBaseAddress attribute of the <compositeDuplex> with the name of the appropriate machine, as shown in the following example.

    <endpoint name = ""  
    ... />  
    <compositeDuplex clientBaseAddress="http://client_machine_name:8000/myClient/" />  

See Also