WSHttpBinding 类

定义

表示一个可互操作的绑定,该绑定支持分布式事务和安全、可靠的会话。

public ref class WSHttpBinding : System::ServiceModel::WSHttpBindingBase
public class WSHttpBinding : System.ServiceModel.WSHttpBindingBase
type WSHttpBinding = class
    inherit WSHttpBindingBase
Public Class WSHttpBinding
Inherits WSHttpBindingBase
继承
派生

示例

下面的示例代码演示如何使用 WSHttpBinding 类。

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.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

注解

WSHttpBindingBasicHttpBinding 相似,但是会提供更多的 Web 服务功能。 它使用 HTTP 传输并提供消息安全,这些与 BasicHttpBinding 一样,但是它还提供事务、可靠消息传递和 WS-Addressing,这些功能可在默认情况下启用或通过单一控制设置来提供。

构造函数

WSHttpBinding()

初始化 WSHttpBinding 类的新实例。

WSHttpBinding(SecurityMode)

通过绑定所使用的指定安全性类型初始化 WSHttpBinding 类的新实例。

WSHttpBinding(SecurityMode, Boolean)

通过绑定所使用的指定安全类型以及一个指示是否启用可靠会话的值来初始化 WSHttpBinding 类的新实例。

WSHttpBinding(String)

使用由配置名称指定的绑定初始化 WSHttpBinding 类的新实例。

属性

AllowCookies

获取或设置一个无论 WCF 客户端是否将自动存储并通过单个网页服务重新发送任何 cookie 的值。

BypassProxyOnLocal

获取或设置一个值,该值指示是否跳过代理服务器而使用本地地址。

(继承自 WSHttpBindingBase)
CloseTimeout

获取或设置在传输引发异常之前可用于关闭连接的时间间隔。

(继承自 Binding)
EnvelopeVersion

获取此绑定处理的消息将要使用的 SOAP 版本。

(继承自 WSHttpBindingBase)
HostNameComparisonMode

获取或设置一个值,该值指示在对 URI 进行匹配时,是否使用主机名来访问服务。

(继承自 WSHttpBindingBase)
MaxBufferPoolSize

获取或设置可为缓冲区管理器分配的最大内存量(以字节为单位),该管理器管理使用此绑定的终结点所需的缓冲区。

(继承自 WSHttpBindingBase)
MaxReceivedMessageSize

获取或设置绑定可处理的消息的最大大小(以字节为单位)。

(继承自 WSHttpBindingBase)
MessageEncoding

获取或设置一个值,该值指示是使用 MTOM 还是文本/XML 对 SOAP 消息进行编码。

(继承自 WSHttpBindingBase)
MessageVersion

获取由绑定所配置的客户端和服务使用的消息版本。

(继承自 Binding)
Name

获取或设置绑定的名称。

(继承自 Binding)
Namespace

获取或设置绑定的 XML 命名空间。

(继承自 Binding)
OpenTimeout

获取或设置在传输引发异常之前可用于打开连接的时间间隔。

(继承自 Binding)
ProxyAddress

获取或设置 HTTP 代理的 URI 地址。

(继承自 WSHttpBindingBase)
ReaderQuotas

获取或设置可由配置了此绑定的终结点处理的 SOAP 消息的复杂性约束。

(继承自 WSHttpBindingBase)
ReceiveTimeout

获取或设置连接在撤消之前保持非活动状态的最大时间间隔,在此时间间隔内未接收任何应用程序消息。

(继承自 Binding)
ReliableSession

获取一个对象,当使用系统提供的一个绑定时,该对象可提供对可用的可靠会话绑定元素属性的便捷访问。

(继承自 WSHttpBindingBase)
Scheme

获取用此绑定配置的通道和侦听器的 URI 传输方案。

(继承自 WSHttpBindingBase)
Security

获取与此绑定一起使用的安全设置。

SendTimeout

获取或设置在传输引发异常之前可用于完成写入操作的时间间隔。

(继承自 Binding)
TextEncoding

获取或设置用于消息文本的字符编码。

(继承自 WSHttpBindingBase)
TransactionFlow

获取或设置一个值,该值指示此绑定是否应支持流动 WS-Transactions。

(继承自 WSHttpBindingBase)
UseDefaultWebProxy

获取或设置一个值,该值指示是否应使用系统的自动配置 HTTP 代理(如果可用)。

(继承自 WSHttpBindingBase)

方法

BuildChannelFactory<TChannel>(BindingParameterCollection)

在客户端上生成通道工厂堆栈,此通道工厂堆栈创建具有指定类型的通道并满足绑定参数集合所指定的功能。

BuildChannelFactory<TChannel>(BindingParameterCollection)

在客户端上生成通道工厂堆栈,此通道工厂堆栈创建具有指定类型的通道并满足绑定参数集合所指定的功能。

(继承自 Binding)
BuildChannelFactory<TChannel>(Object[])

在客户端上生成通道工厂堆栈,该通道工厂堆栈创建具有指定类型的通道并满足对象数组所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足绑定参数集合所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(Uri, BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(Uri, Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。

(继承自 Binding)
BuildChannelListener<TChannel>(Uri, String, Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。

(继承自 Binding)
CanBuildChannelFactory<TChannel>(BindingParameterCollection)

返回一个值,该值指示当前绑定是否可以在客户端上生成满足指定绑定参数集合的通道工厂堆栈。

(继承自 Binding)
CanBuildChannelFactory<TChannel>(Object[])

返回一个值,该值指示当前绑定是否可以在客户端上生成满足对象数组所指定的需求的通道工厂堆栈。

(继承自 Binding)
CanBuildChannelListener<TChannel>(BindingParameterCollection)

返回一个值,该值指示当前绑定是否可以在服务上生成满足指定绑定参数集合的通道侦听器堆栈。

(继承自 Binding)
CanBuildChannelListener<TChannel>(Object[])

返回一个值,该值指示当前绑定是否可以在服务上生成满足对象数组所指定的条件的通道侦听器堆栈。

(继承自 Binding)
CreateBindingElements()

返回一个包含在当前绑定中的已排序的绑定元素集合。

CreateMessageSecurity()

返回来自于当前绑定的安全绑定元素。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetProperty<T>(BindingParameterCollection)

从绑定堆栈的适当层返回所请求的类型化对象(如果存在)。

(继承自 Binding)
GetTransport()

返回来自于当前绑定的传输绑定元素。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ShouldSerializeName()

返回是否应序列化绑定名称。

(继承自 Binding)
ShouldSerializeNamespace()

返回是否应序列化绑定命名空间。

(继承自 Binding)
ShouldSerializeReaderQuotas()

返回一个值,该值指示 ReaderQuotas 属性是否已更改,不再是默认值且应对其进行序列化。

(继承自 WSHttpBindingBase)
ShouldSerializeReliableSession()

返回一个值,该值指示 ReliableSession 属性是否已更改,不再是默认值且应对其进行序列化。

(继承自 WSHttpBindingBase)
ShouldSerializeSecurity()

返回一个值,该值指示 Security 属性是否已更改,不再是默认值且应对其进行序列化。

ShouldSerializeTextEncoding()

返回一个值,该值指示 TextEncoding 属性是否已更改,不再是默认值且应对其进行序列化。

(继承自 WSHttpBindingBase)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IBindingRuntimePreferences.ReceiveSynchronously

获取一个值,该值指示是同步处理传入请求还是异步处理传入请求。

(继承自 WSHttpBindingBase)

适用于