如何:自定义系统提供的绑定

Windows Communication Foundation(WCF)包括多个系统提供的绑定,可用于配置基础绑定元素的某些属性,但并非所有属性。 本主题演示如何设置绑定元素的属性以创建自定义绑定。

有关如何在不使用系统提供的绑定的情况下直接创建和配置绑定元素的详细信息,请参阅 自定义绑定

有关创建和扩展自定义绑定的详细信息,请参阅 扩展绑定

在 WCF 中,所有绑定都由 绑定元素组成。 每个绑定元素都派生自 BindingElement 该类。 系统提供的绑定,例如 BasicHttpBinding 创建和配置其自己的绑定元素。 本主题介绍如何访问和更改这些绑定元素的属性,这些元素不会直接在绑定上公开;具体而言,类 BasicHttpBinding

各个绑定元素包含在由类表示的 BindingElementCollection 集合中,并按以下顺序添加:事务流、可靠会话、安全性、复合双工、单向、流安全性、消息编码和传输。 请注意,并非所有列出的绑定元素都需要在每个绑定中。 用户定义的绑定元素也可以出现在此绑定元素集合中,并且必须按前面所述的相同顺序显示。 例如,用户定义的传输必须是绑定元素集合的最后一个元素。

BasicHttpBinding 类包含三个绑定元素:

  1. 可选的安全绑定元素,即 AsymmetricSecurityBindingElement 与 HTTP 传输(消息级别安全性)一起使用的类, TransportSecurityBindingElement 或者在传输层提供安全性时使用的类(在这种情况下使用 HTTPS 传输)。

  2. 必需的消息编码器绑定元素,TextMessageEncodingBindingElementMtomMessageEncodingBindingElement

  3. 必需的传输绑定元素,必须是HttpTransportBindingElementHttpsTransportBindingElement之一。

在此示例中,我们将创建绑定的实例,从该绑定生成 自定义绑定 ,检查自定义绑定中的绑定元素,并在找到 HTTP 绑定元素时,将其 KeepAliveEnabled 属性设置为 false。 该 KeepAliveEnabled 属性不会直接显示在该 BasicHttpBinding属性上,因此我们必须创建自定义绑定,以便向下导航到绑定元素并设置此属性。

修改系统提供的绑定

  1. 创建类的 BasicHttpBinding 实例并将其安全模式设置为消息级别。

    //  Create an instance of the T:System.ServiceModel.BasicHttpBinding
    //  class and set its security mode to message-level security.
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
    binding.Security.Mode = BasicHttpSecurityMode.Message;
    
    '  Create an instance of the T:System.ServiceModel.BasicHttpBinding 
    '  class and set its security mode to message-level security.
    Dim binding As New BasicHttpBinding()
    With binding.Security
        .Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate
        .Mode = BasicHttpSecurityMode.Message
    End With
    
  2. 从现有绑定创建自定义绑定,并从自定义绑定的某个属性创建 BindingElementCollection 类。

    //  Create a custom binding from the binding
    CustomBinding cb = new CustomBinding(binding);
    //  Get the BindingElementCollection from this custom binding
    BindingElementCollection bec = cb.Elements();
    
    '  Create a custom binding from the binding 
    Dim cb As New CustomBinding(binding)
    '  Get the BindingElementCollection from this custom binding 
    Dim bec = cb.Elements
    
  3. 循环遍历 BindingElementCollection 类,当找到 HttpTransportBindingElement 类时,将其 KeepAliveEnabled 属性设置为 false

    //  Loop through the collection, and when you find the HTTP binding element
    //  set its KeepAliveEnabled property to false
    foreach (BindingElement be in bec)
    {
        Type thisType = be.GetType();
        Console.WriteLine(thisType);
        if (be is HttpTransportBindingElement)
        {
            HttpTransportBindingElement httpElement = (HttpTransportBindingElement)be;
            Console.WriteLine($"\tBefore: HttpTransportBindingElement.KeepAliveEnabled = {httpElement.KeepAliveEnabled}");
            httpElement.KeepAliveEnabled = false;
            Console.WriteLine($"\tAfter:  HttpTransportBindingElement.KeepAliveEnabled = {httpElement.KeepAliveEnabled}");
        }
    }
    
    '  Loop through the collection, and when you find the HTTP binding element
    '  set its KeepAliveEnabled property to false
    For Each be In bec
        Dim thisType = be.GetType()
        Console.WriteLine(thisType)
        If TypeOf be Is HttpTransportBindingElement Then
            Dim httpElement As HttpTransportBindingElement = CType(be, HttpTransportBindingElement)
            Console.WriteLine(Constants.vbTab & "Before: HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled)
            httpElement.KeepAliveEnabled = False
            Console.WriteLine(vbTab & "After:  HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled)
        End If
    Next be
    

另见