Compartilhar via


How to: Customize a System-Provided Binding

Windows Communication Foundation (WCF) includes several system-provided bindings that allow you to configure some of the properties of the underlying binding elements, but not all of the properties. This topic demonstrates how to set properties on the binding elements to create a custom binding.

For more information about how to directly create and configure binding elements without using the system-provided bindings, see Custom Bindings.

For more information about creating and extending custom bindings, see Extending Bindings in Windows Communication Foundation.

In WCF all bindings are made up of binding elements. Each binding element derives from the BindingElement class. System-provided bindings such as BasicHttpBinding create and configure their own binding elements. This topic shows you how to access and change the properties of these binding elements, which are not directly exposed on the binding; specifically, the BasicHttpBinding class.

The individual binding elements are contained in a collection represented by the BindingElementCollection class and are added in this order: Transaction Flow, Reliable Session, Security, Composite Duplex, One-way, Stream Security, Message Encoding, and Transport. Note that not all the binding elements listed are required in every binding. User-defined binding elements can also appear in this binding element collection and must appear in the same order as previously described. For example, a user-defined transport must be the last element of the binding element collection.

The BasicHttpBinding class contains three binding elements:

  1. An optional security binding element, either the AsymmetricSecurityBindingElement class used with the HTTP transport (message level security) or the TransportSecurityBindingElement class, which is used when the transport layer provides security, in which case the HTTPS transport is used.

  2. A required message encoder binding element, either TextMessageEncodingBindingElement or MtomMessageEncodingBindingElement.

  3. A required transport binding element, either HttpTransportBindingElement, or HttpsTransportBindingElement.

In this example we create an instance of the binding, generate a custom binding from it, examine the binding elements in the custom binding, and when we find the HTTP binding element, we set its KeepAliveEnabled property to false. The KeepAliveEnabled property is not exposed directly on the BasicHttpBinding, so we must create a custom binding to navigate down to the binding element and set this property.

To modify a system-provided binding

  1. Create an instance of the BasicHttpBinding class and set its security mode to message-level.

    '  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. Create a custom binding from the binding and create a BindingElementCollection class from one of the custom binding's properties.

    '  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. Loop through the BindingElementCollection class, and when you find the HttpTransportBindingElement class, set its KeepAliveEnabled property to false.

    '  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
    

See Also

Reference

HttpTransportBindingElement
BasicHttpBinding
CustomBinding

Concepts

Custom Bindings


© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21