CustomBinding Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Defines a binding from a list of binding elements.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
The CustomBinding type exposes the following members.
| 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(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, BindingElement[]) | Initializes a new instance of the CustomBinding class from an array of binding elements with a specified name and namespace. |
| 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.) |
| 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>(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>(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.) |
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 Windows Phone:
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 |
// 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 : PhoneApplicationPage
{
//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("http://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;
}


