How to: Customize a System-Provided Binding

In diesem Artikel

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

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

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

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

' 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