W​SHttp​Binding Class

Definition

Represents an interoperable binding that supports distributed transactions and secure, reliable sessions.

public class WSHttpBinding : System.ServiceModel.WSHttpBindingBase
Inheritance
Derived

Inherited Members

System.Object

System.ServiceModel.Channels.Binding

System.ServiceModel.WSHttpBindingBase

Examples

The following sample code shows how to use the WSHttpBinding class.

using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.IdentityModel.Tokens;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
using System.Security.Permissions;

// Define a service contract for the calculator. 
[ServiceContract()]
public interface ICalculator
{
    [OperationContract(IsOneWay = false)]
    double Add(double n1, double n2);
    [OperationContract(IsOneWay = false)]
    double Subtract(double n1, double n2);
    [OperationContract(IsOneWay = false)]
    double Multiply(double n1, double n2);
    [OperationContract(IsOneWay = false)]
    double Divide(double n1, double n2);
}

public sealed class CustomBindingCreator
{

    public static void snippetSecurity()
    {
        WSHttpBinding wsHttpBinding = new WSHttpBinding();
        WSHttpSecurity whSecurity = wsHttpBinding.Security;
    }


    public static void snippetCreateBindingElements()
    {
        WSHttpBinding wsHttpBinding = new WSHttpBinding();
        BindingElementCollection beCollection = wsHttpBinding.CreateBindingElements();
    }


    private void snippetCreateMessageSecurity()
    {
        WSHttpBinding wsHttpBinding = new WSHttpBinding();
        // SecurityBindingElement sbe = wsHttpBinding
    }

    public static void snippetGetTransport()
    {
        WSHttpBinding wsHttpBinding = new WSHttpBinding();
        //		TransportBindingElement tbElement = wsHttpBinding.GetTransport();
    }

    public static void snippetAllowCookies()
    {
        WSHttpBinding wsHttpBinding = new WSHttpBinding();
        wsHttpBinding.AllowCookies = true;
    }

    public static Binding GetBinding()
    {
        // securityMode is Message
        // reliableSessionEnabled is true
        WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message, true);
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

        WSHttpSecurity security = binding.Security;
        return binding;

    }

    public static Binding GetBinding2()
    {
        
        // The security mode is set to Message.
        WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message);
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
        return binding;

    }

    // This method creates a WSFederationHttpBinding.
    public static WSFederationHttpBinding CreateWSFederationHttpBinding()
    {
        // Create an instance of the WSFederationHttpBinding
        WSFederationHttpBinding b = new WSFederationHttpBinding();

        // Set the security mode to Message
        b.Security.Mode = WSFederationHttpSecurityMode.Message;

        // Set the Algorithm Suite to Basic256Rsa15
        b.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Basic256Rsa15;

        // Set NegotiateServiceCredential to true
        b.Security.Message.NegotiateServiceCredential = true;

        // Set IssuedKeyType to Symmetric
        b.Security.Message.IssuedKeyType = SecurityKeyType.SymmetricKey;

        // Set IssuedTokenType to SAML 1.1
        b.Security.Message.IssuedTokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#samlv1.1";

        // Extract the STS certificate from the certificate store
        X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, "cd 54 88 85 0d 63 db ac 92 59 05 af ce b8 b1 de c3 67 9e 3f", false);
        store.Close();

        // Create an EndpointIdentity from the STS certificate
        EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(certs[0]);

        // Set the IssuerAddress using the address of the STS and the previously created EndpointIdentity
        b.Security.Message.IssuerAddress = new EndpointAddress(new Uri("http://localhost:8000/sts/x509"), identity);

        // Set the IssuerBinding to a WSHttpBinding loaded from config
        b.Security.Message.IssuerBinding = new WSHttpBinding("Issuer");

        // Set the IssuerMetadataAddress using the metadata address of the STS and the previously created EndpointIdentity
        b.Security.Message.IssuerMetadataAddress = new EndpointAddress(new Uri("http://localhost:8001/sts/mex"), identity);

        // Create a ClaimTypeRequirement
        ClaimTypeRequirement ctr = new ClaimTypeRequirement("http://example.org/claim/c1", false);

        // Add the ClaimTypeRequirement to ClaimTypeRequirements
        b.Security.Message.ClaimTypeRequirements.Add(ctr);

        // Return the created binding
        return b;
    }

}

// Service class which implements the service contract. 
public class CalculatorService : ICalculator
{
    public double Add(double n1, double n2)
    {
        double result = n1 + n2; return result;
    }
    public double Subtract(double n1, double n2)
    {
        double result = n1 - n2; return result;
    }
    public double Multiply(double n1, double n2)
    {
        double result = n1 * n2; return result;
    }
    public double Divide(double n1, double n2)
    {
        double result = n1 / n2; return result;
    }


    // Host the service within this EXE console application. 
    public static void Main()
    {
        // Create a WSHttpBinding and set its property values. 
        WSHttpBinding binding = new WSHttpBinding();
        binding.Name = "binding1";
        binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
        binding.Security.Mode = SecurityMode.Message;
        binding.ReliableSession.Enabled = false;
        binding.TransactionFlow = false;
        //Specify a base address for the service endpoint. 
        Uri baseAddress = new Uri(@"http://localhost:8000/servicemodelsamples/service");
        // Create a ServiceHost for the CalculatorService type 
        // and provide it with a base address. 
        ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
        serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, baseAddress);
        // Open the ServiceHostBase to create listeners 
        // and start listening for messages. 
        serviceHost.Open();
        // The service can now be accessed. 
        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press <ENTER> to terminate service.");
        Console.WriteLine(); Console.ReadLine();
        // Close the ServiceHost to shutdown the service. 
        serviceHost.Close();
    }
}

Imports System
Imports System.ServiceModel
Imports System.Collections.Generic
Imports System.IdentityModel.Tokens
Imports System.Security.Cryptography.X509Certificates
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Security
Imports System.ServiceModel.Security.Tokens
Imports System.Security.Permissions

' Define a service contract for the calculator. 
<ServiceContract()> _
Public Interface ICalculator
	<OperationContract(IsOneWay := False)> _
	Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
	<OperationContract(IsOneWay := False)> _
	Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
	<OperationContract(IsOneWay := False)> _
	Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
	<OperationContract(IsOneWay := False)> _
	Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
End Interface

Public NotInheritable Class CustomBindingCreator

	Public Shared Sub snippetSecurity()
		Dim wsHttpBinding As New WSHttpBinding()
		Dim whSecurity As WSHttpSecurity = wsHttpBinding.Security
	End Sub


	Public Shared Sub snippetCreateBindingElements()
		Dim wsHttpBinding As New WSHttpBinding()
		Dim beCollection As BindingElementCollection = wsHttpBinding.CreateBindingElements()
	End Sub


	Private Sub snippetCreateMessageSecurity()
		Dim wsHttpBinding As New WSHttpBinding()
	End Sub

	Public Shared Sub snippetGetTransport()
		Dim wsHttpBinding As New WSHttpBinding()
	End Sub

	Public Shared Sub snippetAllowCookies()
		Dim wsHttpBinding As New WSHttpBinding()
		wsHttpBinding.AllowCookies = True
	End Sub

	Public Shared Function GetBinding() As Binding
		' securityMode is Message
		' reliableSessionEnabled is true
		Dim binding As New WSHttpBinding(SecurityMode.Message, True)
		binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows

		Dim security As WSHttpSecurity = binding.Security
		Return binding

	End Function

	Public Shared Function GetBinding2() As Binding

		' The security mode is set to Message.
		Dim binding As New WSHttpBinding(SecurityMode.Message)
		binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows
		Return binding

	End Function

	' This method creates a WSFederationHttpBinding.
	Public Shared Function CreateWSFederationHttpBinding() As WSFederationHttpBinding
		' Create an instance of the WSFederationHttpBinding
		Dim b As New WSFederationHttpBinding()

		' Set the security mode to Message
		b.Security.Mode = WSFederationHttpSecurityMode.Message

		' Set the Algorithm Suite to Basic256Rsa15
		b.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Basic256Rsa15

		' Set NegotiateServiceCredential to true
		b.Security.Message.NegotiateServiceCredential = True

		' Set IssuedKeyType to Symmetric
		b.Security.Message.IssuedKeyType = SecurityKeyType.SymmetricKey

		' Set IssuedTokenType to SAML 1.1
		b.Security.Message.IssuedTokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#samlv1.1"

		' Extract the STS certificate from the certificate store
		Dim store As New X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser)
		store.Open(OpenFlags.ReadOnly)
        Dim certs As X509Certificate2Collection = store.Certificates.Find(X509FindType.FindByThumbprint, "cd 54 88 85 0d 63 db ac 92 59 05 af ce b8 b1 de c3 67 9e 3f", False)
		store.Close()

		' Create an EndpointIdentity from the STS certificate
		Dim identity As EndpointIdentity = EndpointIdentity.CreateX509CertificateIdentity(certs(0))

		' Set the IssuerAddress using the address of the STS and the previously created EndpointIdentity
		b.Security.Message.IssuerAddress = New EndpointAddress(New Uri("http://localhost:8000/sts/x509"), identity)

		' Set the IssuerBinding to a WSHttpBinding loaded from config
		b.Security.Message.IssuerBinding = New WSHttpBinding("Issuer")

		' Set the IssuerMetadataAddress using the metadata address of the STS and the previously created EndpointIdentity
		b.Security.Message.IssuerMetadataAddress = New EndpointAddress(New Uri("http://localhost:8001/sts/mex"), identity)

		' Create a ClaimTypeRequirement
		Dim ctr As New ClaimTypeRequirement("http://example.org/claim/c1", False)

		' Add the ClaimTypeRequirement to ClaimTypeRequirements
		b.Security.Message.ClaimTypeRequirements.Add(ctr)

		' Return the created binding
		Return b
	End Function

End Class

' Service class which implements the service contract. 
Public Class CalculatorService
	Implements ICalculator
	Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
        Dim result = n1 + n2
		Return result
	End Function
	Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
        Dim result = n1 - n2
		Return result
	End Function
	Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
        Dim result = n1 * n2
		Return result
	End Function
	Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
        Dim result = n1 / n2
		Return result
	End Function


	' Host the service within this EXE console application. 
	Public Shared Sub Main()
		' Create a WSHttpBinding and set its property values. 
        Dim binding As New WSHttpBinding()
        With binding
            .Name = "binding1"
            .HostNameComparisonMode = HostNameComparisonMode.StrongWildcard
            .Security.Mode = SecurityMode.Message
            .ReliableSession.Enabled = False
            .TransactionFlow = False
        End With
		
		'Specify a base address for the service endpoint. 
		Dim baseAddress As New Uri("http://localhost:8000/servicemodelsamples/service")
		' Create a ServiceHost for the CalculatorService type 
		' and provide it with a base address. 
		Dim serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)
		serviceHost.AddServiceEndpoint(GetType(ICalculator), binding, baseAddress)
		' Open the ServiceHostBase to create listeners 
		' and start listening for messages. 
		serviceHost.Open()
		' The service can now be accessed. 
		Console.WriteLine("The service is ready.")
		Console.WriteLine("Press <ENTER> to terminate service.")
		Console.WriteLine()
		Console.ReadLine()
		' Close the ServiceHost to shutdown the service. 
		serviceHost.Close()
	End Sub
End Class

Remarks

The WSHttpBinding is similar to the BasicHttpBinding but provides more Web service features. It uses the HTTP transport and provides message security, as does BasicHttpBinding, but it also provides transactions, reliable messaging, and WS-Addressing, either enabled by default or available through a single control setting.

Constructors

W​SHttp​Binding()

Initializes a new instance of the WSHttpBinding class.

W​SHttp​Binding(​Security​Mode)

Initializes a new instance of the WSHttpBinding class with a specified type of security used by the binding.

W​SHttp​Binding(​String)

Initializes a new instance of the WSHttpBinding class with a binding specified by its configuration name.

W​SHttp​Binding(​Security​Mode, ​Boolean)

Initializes a new instance of the WSHttpBinding class with a specified type of security used by the binding and a value that indicates whether a reliable session is enabled.

Properties

Allow​Cookies

Gets or sets a value that indicates whether the WCF client will automatically store and resend any cookies sent by a single web service.

Security

Gets the security settings used with this binding.

Methods

Build​Channel​Factory<​TChannel>(​Binding​Parameter​Collection)

Builds the channel factory stack on the client that creates a specified type of channel and that satisfies the features specified by a collection of binding parameters.

Create​Binding​Elements()

Returns an ordered collection of binding elements contained in the current binding.

Create​Message​Security()

Returns the security binding element from the current binding.

Get​Transport()

Returns the transport binding element from the current binding.

Should​Serialize​Security()

Returns a value that indicates whether the Security property has changed from its default value and should be serialized.