Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
The BasicHttpBinding class contains three binding elements:
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.
A required message encoder binding element, either TextMessageEncodingBindingElement or MtomMessageEncodingBindingElement.
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.
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.
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
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 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
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
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Create a UI that uses data binding in .NET MAUI. - Training
Create a UI with data binding. Your UI automatically updates based on the latest data, while the data updates in response to changes in the UI.
Documentation
Learn more about: Custom Bindings
How to: Create a Custom Binding Using the SecurityBindingElement - WCF
Learn more about: How to: Create a Custom Binding Using the SecurityBindingElement
Custom Binding Transport and Encoding - WCF
Learn more about: Custom Binding Transport and Encoding