CustomBinding Class

Definition

Defines a binding from a list of binding elements.

public class CustomBinding : System.ServiceModel.Channels.Binding
Inheritance
CustomBinding

Examples

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();
}
Dim baseAddress As New Uri("http://localhost:8000/servicemodelsamples/service")

' Create a ServiceHost for the CalculatorService type and provide the base address.
Using serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)
	' Create a custom binding that contains two binding elements.
	Dim reliableSession As New ReliableSessionBindingElement()
	reliableSession.Ordered = True

	Dim httpTransport As New HttpTransportBindingElement()
	httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous
	httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard

	Dim binding As New CustomBinding(reliableSession, httpTransport)

	' Add an endpoint using that binding.
	serviceHost.AddServiceEndpoint(GetType(ICalculator), binding, "")

	' Add a MEX endpoint.
	Dim smb As 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()
End Using

Remarks

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:

The following table summarizes the options for each layer.

Layer Options Required
Transaction Flow TransactionFlowBindingElement No
Reliability ReliableSessionBindingElement No
Security Symmetric, Asymmetric, Transport-Level No
Shape Change CompositeDuplexBindingElement 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.

Note

When calling a WCF service built with.Net Framework 4.0 from a WCF client application built with .NET Framework 3.5 or earlier, the configuration file generated by svcutil.exe or adding a service reference from Visual Studio will contain the validity attribute in the binding configuration. This attribute is not recognized by the .NET Framework 3.5 runtime and the application will thrown an ConfigurationErrorsException with the message "Unrecognized attribute validity". To workaround this problem, remove the validity attribute from the binding configuration.

Constructors

CustomBinding()

Initializes a new instance of the CustomBinding class.

CustomBinding(IEnumerable<BindingElement>)

Initializes a new instance of the CustomBinding class with the binding elements from a complete channel stack.

CustomBinding(Binding)

Initializes a new instance of the CustomBinding class from the values of a specified binding.

CustomBinding(BindingElement[])

Initializes a new instance of the CustomBinding class from an array of binding elements.

CustomBinding(String)

Initializes a new instance of the CustomBinding class.

CustomBinding(String, String, BindingElement[])

Initializes a new instance of the CustomBinding class from an array of binding elements with a specified name and namespace.

Properties

Elements

Gets the binding elements from the custom binding.

Scheme

Gets the URI scheme for transport used by the custom binding.

Methods

CreateBindingElements()

Returns a generic collection of the binding elements from the custom binding.

Equals(Object) Inherited from Object
Equals(Object, Object) Inherited from Object
GetHashCode() Inherited from Object
GetType() Inherited from Object
MemberwiseClone() Inherited from Object
ReferenceEquals(Object, Object) Inherited from Object
ToString() Inherited from Object
BuildChannelFactory<TChannel>(Object[]) Inherited from Binding
BuildChannelFactory<TChannel>(BindingParameterCollection) Inherited from Binding
BuildChannelListener<TChannel>(Object[]) Inherited from Binding
BuildChannelListener<TChannel>(BindingParameterCollection) Inherited from Binding
BuildChannelListener<TChannel>(Uri, Object[]) Inherited from Binding
BuildChannelListener<TChannel>(Uri, BindingParameterCollection) Inherited from Binding
BuildChannelListener<TChannel>(Uri, String, Object[]) Inherited from Binding
BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection) Inherited from Binding
BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[]) Inherited from Binding
BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection) Inherited from Binding
CanBuildChannelFactory<TChannel>(Object[]) Inherited from Binding
CanBuildChannelFactory<TChannel>(BindingParameterCollection) Inherited from Binding
CanBuildChannelListener<TChannel>(Object[]) Inherited from Binding
CanBuildChannelListener<TChannel>(BindingParameterCollection) Inherited from Binding
CloseTimeout Inherited from Binding
GetProperty<T>(BindingParameterCollection) Inherited from Binding
MessageVersion Inherited from Binding
Name Inherited from Binding
Namespace Inherited from Binding
OpenTimeout Inherited from Binding
ReceiveTimeout Inherited from Binding
SendTimeout Inherited from Binding
ShouldSerializeName() Inherited from Binding
ShouldSerializeNamespace() Inherited from Binding

Applies to