MSDN Logo

Microsoft Docs

WSHttpBinding

The WSHttpBinding is the most commonly used binding for SOAP‑based services that require security, reliability and interoperability with other platforms. It implements WS‑* specifications (WS‑Security, WS‑ReliableMessaging, WS‑Policy, etc.) and is the default binding for WCF services exposed via HTTP.

Key Features

  • Transport security (HTTPS) or message security (WS‑Security).
  • Supports reliable sessions (WS‑ReliableMessaging).
  • Built‑in support for WS‑Trust and WS‑Federation.
  • Negotiable security modes: None, Transport, Message, TransportWithMessageCredential.
  • Configurable ReliableSession and TransactionFlow.

Creating a Service with WSHttpBinding (C#)

using System;
using System.ServiceModel;

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    int Add(int a, int b);
}

public class CalculatorService : ICalculator
{
    public int Add(int a, int b) => a + b;
}

class Program
{
    static void Main()
    {
        var baseAddress = new Uri("http://localhost:8080/CalculatorService");
        var binding = new WSHttpBinding(SecurityMode.Message);
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

        using (var host = new ServiceHost(typeof(CalculatorService), baseAddress))
        {
            host.AddServiceEndpoint(typeof(ICalculator), binding, "");
            host.Open();
            Console.WriteLine("Service is running...");
            Console.ReadKey();
        }
    }
}

Configuration (app.config)

<configuration>
  <system.serviceModel>
    <services>
      <service name="CalculatorService">
        <endpoint address="" binding="wsHttpBinding"
                  bindingConfiguration="SecureBinding"
                  contract="ICalculator" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/CalculatorService" />
          </baseAddresses>
        </host>
      </service>
    </services>

    <bindings>
      <wsHttpBinding>
        <binding name="SecureBinding"
                 securityMode="Message">
          <security>
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

Consuming the Service (C# Client)

using System;
using System.ServiceModel;

class Client
{
    static void Main()
    {
        var binding = new WSHttpBinding(SecurityMode.Message);
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
        var endpoint = new EndpointAddress("http://localhost:8080/CalculatorService");

        var factory = new ChannelFactory<ICalculator>(binding, endpoint);
        var proxy = factory.CreateChannel();

        int result = proxy.Add(5, 7);
        Console.WriteLine($""5 + 7 = {result}"");
        ((IClientChannel)proxy).Close();
        factory.Close();
    }
}

Testing the Service

Run the service host, then execute the client. You should see the output:

5 + 7 = 12

Use WCF Test Client for exploratory testing.

Further Reading