How to: Create a Custom Reliable Session Binding with HTTPS

This topic demonstrates the use of Secure Sockets Layer (SSL) transport security with reliable sessions. To use a reliable session over HTTPS, you must create a custom binding that uses a reliable session and the HTTPS transport. You enable the reliable session either imperatively by using code or declaratively in the configuration file. This procedure uses the client and service configuration files to enable the reliable session and the <httpsTransport> element.

The key part of this procedure is that the <endpoint> configuration element contain a bindingConfiguration attribute that references a custom binding configuration named reliableSessionOverHttps. The <binding> configuration element references this name to specify that a reliable session and the HTTPS transport are used by including <reliableSession> and <httpsTransport> elements.

For the source copy of this example, see Custom Binding Reliable Session over HTTPS.

Configure the service with a CustomBinding to use a reliable session with HTTPS

  1. Define a service contract for the type of service.

    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
    
  2. Implement the service contract in a service class. Note that the address or binding information isn't specified inside the implementation of the service. You aren't required to write code to retrieve the address or binding information from the configuration file.

    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    } 
    
  3. Create a Web.config file to configure an endpoint for the CalculatorService with a custom binding named reliableSessionOverHttps that uses a reliable session and the HTTPS transport.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <services>
          <service name="Microsoft.ServiceModel.Samples.CalculatorService" 
                   behaviorConfiguration="CalculatorServiceBehavior">
            <!-- 
              Use base address provided by the host 
            -->
            <endpoint address=""
                      binding="customBinding"
                      bindingConfiguration="reliableSessionOverHttps" 
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />
            <!-- 
              The mex endpoint is exposed as 
              http://localhost/servicemodelsamples/service.svc/mex
            -->
            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>
    
        <bindings>
          <customBinding>
            <binding name="reliableSessionOverHttps">
              <reliableSession />
              <httpsTransport />
            </binding>
          </customBinding>
        </bindings>
    
      </system.serviceModel>
    </configuration>
    
  4. Create a Service.svc file that contains the line:

    <%@ServiceHost language=c# Service="CalculatorService" %>
    
  5. Place the Service.svc file in your Internet Information Services (IIS) virtual directory.

Configure the client with a CustomBinding to use a reliable session with HTTPS

  1. Use the ServiceModel Metadata Utility Tool (Svcutil.exe) from the command line to generate code from service metadata.

    Svcutil.exe <Metadata Exchange (MEX) address or HTTP GET address>
    
  2. The client that's generated contains the ICalculator interface that defines the service contract that the client implementation must satisfy.

    // Generated interface defining the ICalculator contract	
    [System.ServiceModel.ServiceContractAttribute(
    Namespace="http://Microsoft.ServiceModel.Samples", 
    ConfigurationName="Microsoft.ServiceModel.Samples.ICalculator")]
    public interface ICalculator
    {
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add", 
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
        double Add(double n1, double n2);
            
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", 
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
        double Subtract(double n1, double n2);
            
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", 
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
        double Multiply(double n1, double n2);
            
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", 
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
        double Divide(double n1, double n2);
    }
    
  3. The generated client application also contains the implementation of the ClientCalculator. Note that the address and binding information isn't specified inside the implementation of the service. You aren't required to write code to retrieve the address and binding information from the configuration file.

    // Implementation of the CalculatorClient
    public partial class CalculatorClient : 
        System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>, 
        Microsoft.ServiceModel.Samples.ICalculator
    {
        public CalculatorClient()
        {
        }
        
        public CalculatorClient(string endpointConfigurationName) : 
            base(endpointConfigurationName)
        {
        }
        
        public CalculatorClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public CalculatorClient(string endpointConfigurationName, 
            System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public CalculatorClient(System.ServiceModel.Channels.Binding binding, 
            System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
        {
        }
        
        public double Add(double n1, double n2)
        {
            return base.Channel.Add(n1, n2);
        }
        
        public double Subtract(double n1, double n2)
        {
            return base.Channel.Subtract(n1, n2);
        }
        
        public double Multiply(double n1, double n2)
        {
            return base.Channel.Multiply(n1, n2);
        }
        
        public double Divide(double n1, double n2)
        {
            return base.Channel.Divide(n1, n2);
        }
    }
    
  4. Configure a custom binding named reliableSessionOverHttps to use the HTTPS transport and reliable sessions.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <client>
          <!-- 
            This endpoint has an https address
          -->
          <endpoint name=""
                    address="https://localhost/servicemodelsamples/service.svc" 
                    binding="customBinding" 
                    bindingConfiguration="reliableSessionOverHttps" 
                    contract="Microsoft.ServiceModel.Samples.ICalculator" />
        </client>
    
        <bindings>
          <customBinding>
            <binding name="reliableSessionOverHttps">
              <reliableSession />
              <httpsTransport />
            </binding>
          </customBinding>      
        </bindings>
    
      </system.serviceModel>
    </configuration>
    
  5. Create an instance of the ClientCalculator in an application and then call the service operations.

    //Client implementation code.
    class Client
    {
        static void Main()
        {
            // Create a client with given client endpoint configuration
            CalculatorClient client = new CalculatorClient();
    
            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
    
            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
    
            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
    
            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
    
            //Closing the client gracefully closes the connection and cleans up resources
            client.Close();
    
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
    
  6. Compile and run the client.

.NET Framework security

Because the certificate used in this sample is a test certificate created with Makecert.exe, a security alert appears when you try to access an HTTPS address, such as https://localhost/servicemodelsamples/service.svc, from your browser.

See also

Reliable Sessions