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 地址,默认启用或通过单个控件设置提供。

构造函数

名称 说明
WSHttpBinding()

初始化 WSHttpBinding 类的新实例。

WSHttpBinding(SecurityMode, Boolean)

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

WSHttpBinding(SecurityMode)

使用绑定使用的指定安全类型初始化类的新实例 WSHttpBinding

WSHttpBinding(String)

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

属性

名称 说明
AllowCookies

获取或设置一个值,该值指示 WCF 客户端是否会自动存储和重新发送单个 Web 服务发送的任何 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)

适用于