Export (0) Print
Expand All

CustomBinding Class

Defines a binding from a list of binding elements.

System.Object
  System.ServiceModel.Channels.Binding
    System.ServiceModel.Channels.CustomBinding

Namespace:  System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public class CustomBinding : Binding

The CustomBinding type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneCustomBinding()Initializes a new instance of the CustomBinding class.
Public methodSupported by Silverlight for Windows PhoneCustomBinding(Binding)Initializes a new instance of the CustomBinding class from the values of a specified binding.
Public methodSupported by Silverlight for Windows PhoneCustomBinding(BindingElement[])Initializes a new instance of the CustomBinding class from an array of binding elements.
Public methodSupported by Silverlight for Windows PhoneCustomBinding(IEnumerable<BindingElement>)Initializes a new instance of the CustomBinding class with the binding elements from a complete channel stack.
Public methodSupported by Silverlight for Windows PhoneCustomBinding(String, String, BindingElement[])Initializes a new instance of the CustomBinding class from an array of binding elements with a specified name and namespace.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneCloseTimeoutGets or sets the interval of time provided for a connection to close before the transport raises an exception. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows PhoneElementsGets the binding elements from the custom binding.
Public propertySupported by Silverlight for Windows PhoneMessageVersionGets the message version used by clients and services configured with the binding. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows PhoneNameGets or sets the name of the binding. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows PhoneNamespaceGets or sets the XML namespace of the binding. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows PhoneOpenTimeoutGets or sets the interval of time provided for a connection to open before the transport raises an exception. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows PhoneReceiveTimeoutGets 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.)
Public propertySupported by Silverlight for Windows PhoneSchemeGets the URI scheme for transport used by the custom binding. (Overrides Binding.Scheme.)
Public propertySupported by Silverlight for Windows PhoneSendTimeoutGets or sets the interval of time provided for a write operation to complete before the transport raises an exception. (Inherited from Binding.)
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneBuildChannelFactory<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.)
Public methodSupported by Silverlight for Windows PhoneBuildChannelFactory<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.)
Public methodSupported by Silverlight for Windows PhoneCanBuildChannelFactory<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.)
Public methodSupported by Silverlight for Windows PhoneCanBuildChannelFactory<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.)
Public methodSupported by Silverlight for Windows PhoneCreateBindingElementsReturns a generic collection of the binding elements from the custom binding. (Overrides Binding.CreateBindingElements().)
Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetProperty<T>Returns a typed object requested, if present, from the appropriate layer in the binding stack. (Inherited from Binding.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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:

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 : 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("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;
        }



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

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft