CustomBinding Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Defines a binding from a list of binding elements.
Inheritance Hierarchy
System.Object
System.ServiceModel.Channels.Binding
System.ServiceModel.Channels.CustomBinding
Namespace: System.ServiceModel.Channels
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Syntax
'Declaration
Public Class CustomBinding _
Inherits Binding
public class CustomBinding : Binding
The CustomBinding type exposes the following members.
Constructors
Name | Description | |
---|---|---|
CustomBinding() | Initializes a new instance of the CustomBinding class. | |
CustomBinding(Binding) | Initializes a new instance of the CustomBinding class from the values of a specified binding. | |
CustomBinding(array<BindingElement[]) | Initializes a new instance of the CustomBinding class from an array of binding elements. | |
CustomBinding(IEnumerable<BindingElement>) | Initializes a new instance of the CustomBinding class with the binding elements from a complete channel stack. | |
CustomBinding(String, String, array<BindingElement[]) | Initializes a new instance of the CustomBinding class from an array of binding elements with a specified name and namespace. |
Top
Properties
Name | Description | |
---|---|---|
CloseTimeout | Gets or sets the interval of time provided for a connection to close before the transport raises an exception. (Inherited from Binding.) | |
Elements | Gets the binding elements from the custom binding. | |
MessageVersion | Gets the message version used by clients and services configured with the binding. (Inherited from Binding.) | |
Name | Gets or sets the name of the binding. (Inherited from Binding.) | |
Namespace | Gets or sets the XML namespace of the binding. (Inherited from Binding.) | |
OpenTimeout | Gets or sets the interval of time provided for a connection to open before the transport raises an exception. (Inherited from Binding.) | |
ReceiveTimeout | Gets or sets the interval of time that a connection can remain inactive, during which no application messages are received, before it is dropped. (Inherited from Binding.) | |
Scheme | Gets the URI scheme for transport used by the custom binding. (Overrides Binding.Scheme.) | |
SendTimeout | Gets or sets the interval of time provided for a write operation to complete before the transport raises an exception. (Inherited from Binding.) |
Top
Methods
Name | Description | |
---|---|---|
BuildChannelFactory<TChannel>(BindingParameterCollection) | Builds the channel factory stack on the client that creates a specified type of channel and that satisfies the features specified by a collection of binding parameters. (Inherited from Binding.) | |
BuildChannelFactory<TChannel>(array<Object[]) | Builds the channel factory stack on the client that creates a specified type of channel and that satisfies the features specified by an object array. (Inherited from Binding.) | |
CanBuildChannelFactory<TChannel>(BindingParameterCollection) | Returns a value that indicates whether the current binding can build a channel factory stack on the client that satisfies the collection of binding parameters specified. (Inherited from Binding.) | |
CanBuildChannelFactory<TChannel>(array<Object[]) | Returns a value that indicates whether the current binding can build a channel factory stack on the client that satisfies the requirements specified by an object array. (Inherited from Binding.) | |
CreateBindingElements | Returns a generic collection of the binding elements from the custom binding. (Overrides Binding.CreateBindingElements().) | |
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetProperty<T> | Returns a typed object requested, if present, from the appropriate layer in the binding stack. (Inherited from Binding.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Top
Remarks
Use a custom binding when one of the system-provided bindings does not meet the requirements of your client. A custom binding could be used, for example, to enable the use of a new encoder to access 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 bottom of this stack are the binding elements that specify the message encoder and the transport:
At the bottom is a required transport element. You can use your own transport or use one of the HTTP transport binding elements provided by Silverlight 5:
Next is a required message encoding binding element. You can use your own transport or use one of the following message encoding bindings:
The following table summarizes the options for each layer.
Layer |
Options |
Required |
---|---|---|
Encoding |
Text, Custom |
Yes |
Transport |
HTTP, HTTPS, Custom |
Yes |
Examples
' This is an example of how to create a custom binding with a specified encoding and tansport,
' and how to create and open a channel for the service endpoint configured with the custom binding.
Partial Public Class MainPage
Inherits UserControl
'Declare a separate UI thread.
Private uiThread As SynchronizationContext
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'Create a custom binding that uses HTTP and binary message encoding.
Dim elements = New List(Of BindingElement)()
elements.Add(New BinaryMessageEncodingBindingElement())
elements.Add(New HttpTransportBindingElement())
Dim binding = New CustomBinding(elements)
'Put the binding elements from the custom binding into a collection
' or for inspection or modification.
Dim bindingElementCollection As BindingElementCollection = binding.Elements
'Check on the types of binding elements contained in the collection.
Dim boolHTBE As Boolean = bindingElementCollection.Contains(GetType(HttpTransportBindingElement))
'Returns true.
Dim txtboolHTBE As String = boolHTBE.ToString()
txtOutput1.Text = txtboolHTBE 'Bind to text output.
Dim boolHSTBE As Boolean = bindingElementCollection.Contains(GetType(HttpsTransportBindingElement))
'Returns false.
Dim txtboolHSTBE As String = boolHSTBE.ToString()
txtOutput2.Text = txtboolHSTBE 'Bind to text output.
'Create a channel factory for the service endpoint configured with the custom binding.
Dim cf = New ChannelFactory(Of IService1)(binding, New EndpointAddress("https://localhost:10483/Service1.svc"))
'Save the synchronized context for the UI thread.
uiThread = SynchronizationContext.Current
'Open the channel.
Dim channel As IService1 = cf.CreateChannel()
'Invoke the GetPerson method on the service asynchronously. The signature is:
'IAsyncResult BeginGetPerson(int personId, AsyncCallback callback, object state)
channel.BeginGetPerson(4, AddressOf GetPersonCallback, channel)
End Sub
'Implement the callback.
Private Sub GetPersonCallback(ByVal asyncResult As IAsyncResult)
Dim p As Person = (CType(asyncResult.AsyncState, IService1)).EndGetPerson(asyncResult)
'Update the UI thread and its output with the result of the method call.
uiThread.Post(AddressOf UpdateUI, p)
End Sub
'Update the result displayed for the GetPerson invocation.
Private Sub UpdateUI(ByVal state As Object)
Dim p As Person = CType(state, Person)
resultPersonName.Text = "The name of the Person is: " & p.Name
resultPersonAge.Text = "The age of the Person is: " & p.Age
End Sub
// This is an example of how to create a custom binding with a specified encoding and tansport,
// and how to create and open a channel for the service endpoint configured with the custom binding.
public partial class MainPage : UserControl
{
//Declare a separate UI thread.
SynchronizationContext uiThread;
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//Create a custom binding that uses HTTP and binary message encoding.
var elements = new List<BindingElement>();
elements.Add(new BinaryMessageEncodingBindingElement());
elements.Add(new HttpTransportBindingElement());
var binding = new CustomBinding(elements);
//Put the binding elements from the custom binding into a collection
// or for inspection or modification.
BindingElementCollection bindingElementCollection = binding.Elements;
//Check on the types of binding elements contained in the collection.
bool boolHTBE = bindingElementCollection.Contains(typeof(HttpTransportBindingElement));
//Returns true.
string txtboolHTBE = boolHTBE.ToString();
txtOutput1.Text = txtboolHTBE; //Bind to text output.
bool boolHSTBE = bindingElementCollection.Contains(typeof(HttpsTransportBindingElement));
//Returns false.
string txtboolHSTBE = boolHSTBE.ToString();
txtOutput2.Text = txtboolHSTBE; //Bind to text output.
//Create a channel factory for the service endpoint configured with the custom binding.
var cf = new ChannelFactory<IService1>(binding, new EndpointAddress("https://localhost:10483/Service1.svc"));
//Save the synchronized context for the UI thread.
uiThread = SynchronizationContext.Current;
//Open the channel.
IService1 channel = cf.CreateChannel();
//Invoke the GetPerson method on the service asynchronously. The signature is:
//IAsyncResult BeginGetPerson(int personId, AsyncCallback callback, object state)
channel.BeginGetPerson(4, GetPersonCallback, channel);
}
//Implement the callback.
void GetPersonCallback(IAsyncResult asyncResult)
{
Person p = ((IService1)asyncResult.AsyncState).EndGetPerson(asyncResult);
//Update the UI thread and its output with the result of the method call.
uiThread.Post(UpdateUI, p);
}
//Update the result displayed for the GetPerson invocation.
private void UpdateUI(object state)
{
Person p = (Person)state;
resultPersonName.Text = "The name of the Person is: " + p.Name;
resultPersonAge.Text = "The age of the Person is: " + p.Age;
}
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.