CustomBinding Class
Defines a binding from a list of binding elements.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Use a custom binding when one of the system-provided bindings does not meet the requirements of your service. A custom binding could be used, for example, to enable the use of a new transport or a new encoder at a service endpoint.
A custom binding is constructed using one of the CustomBinding from a collection of binding elements that are "stacked" in a specific order:
At the top is an optional TransactionFlowBindingElement that allows flowing transactions.
Next is an optional ReliableSessionBindingElement that provides a session and ordering mechanism as defined in the WS-ReliableMessaging specification. This notion of a session can cross SOAP and transport intermediaries.
Next is an optional security binding element that provides security features like authorization, authentication, protection, and confidentiality. The following security binding elements are provided by Windows Communication Foundation (WCF):
Next are the optional message-patterns specified by binding elements:
Next are the optional transport upgrades/helpers binding elements:
Next is a required message encoding binding element. You can use your own transport or use one of the following message encoding bindings:
At the bottom is a required transport element. You can use your own transport or use one of transport binding elements provided by Windows Communication Foundation (WCF):
The following table summarizes the options for each layer.
Layer | Options | Required |
|---|---|---|
Transaction Flow | No | |
Reliability | No | |
Security | Symmetric, Asymmetric, Transport-Level | No |
Shape Change | No | |
Transport Upgrades | SSL stream, Windows stream, Peer Resolver | No |
Encoding | Text, Binary, MTOM, Custom | Yes |
Transport | TCP, Named Pipes, HTTP, HTTPS, flavors of MSMQ, Custom | Yes |
In addition, you can define your own binding elements and insert them between any of the preceding defined layers.
For a discussion on how to use a custom binding to modify a system-provided binding, see How To: Customize a System-Provided Binding.
The following example shows how to create a CustomBinding object using a ReliableSessionBindingElement and an HttpTransportBindingElement
Uri baseAddress = new Uri("http://localhost:8000/servicemodelsamples/service"); // Create a ServiceHost for the CalculatorService type and provide the base address. using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress)) { // Create a custom binding that contains two binding elements. ReliableSessionBindingElement reliableSession = new ReliableSessionBindingElement(); reliableSession.Ordered = true; HttpTransportBindingElement httpTransport = new HttpTransportBindingElement(); httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous; httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; CustomBinding binding = new CustomBinding(reliableSession, httpTransport); // Add an endpoint using that binding. serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, ""); // Add a MEX endpoint. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; smb.HttpGetUrl = new Uri("http://localhost:8001/servicemodelsamples"); serviceHost.Description.Behaviors.Add(smb); // Open the ServiceHostBase to create listeners and start listening for messages. serviceHost.Open(); // The service can now be accessed. Console.WriteLine("The service is ready."); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); // Close the ServiceHostBase to shutdown the service. serviceHost.Close(); }
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.