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

Windows Communication Foundation (WCF) 包含若干个系统提供的绑定,这些绑定允许你配置基础绑定元素的某些属性,但不是全部的属性。 本主题演示如何设置绑定元素上的属性以创建自定义绑定。

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

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

在 WCF 中,所有绑定都由绑定元素组成。 每个绑定元素都是从 BindingElement 类派生的。 系统提供的绑定(如 BasicHttpBinding)可创建和配置各自的绑定元素。 本主题演示如何访问和更改这些绑定元素的属性,这些属性不会直接在绑定上公开,具体而言,这些属性不会直接在 BasicHttpBinding 类上公开。

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

BasicHttpBinding 类包含三个绑定元素:

  1. 可选的安全绑定元素,即,与 HTTP 传输协议(消息级安全)一起使用的 AsymmetricSecurityBindingElement 类,或在传输协议层提供安全性时使用的 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 = {0}", httpElement.KeepAliveEnabled);
            httpElement.KeepAliveEnabled = false;
            Console.WriteLine("\tAfter:  HttpTransportBindingElement.KeepAliveEnabled = {0}", 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
    

另请参阅