Condividi tramite


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
Public methodSupported by Silverlight for Windows Phone CustomBinding() Initializes a new instance of the CustomBinding class.
Public methodSupported by Silverlight for Windows Phone CustomBinding(Binding) Initializes a new instance of the CustomBinding class from the values of a specified binding.
Public methodSupported by Silverlight for Windows Phone CustomBinding(array<BindingElement[]) Initializes a new instance of the CustomBinding class from an array of binding elements.
Public methodSupported by Silverlight for Windows Phone CustomBinding(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 Phone 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
Public propertySupported by Silverlight for Windows Phone CloseTimeout Gets 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 Phone Elements Gets the binding elements from the custom binding.
Public propertySupported by Silverlight for Windows Phone MessageVersion Gets the message version used by clients and services configured with the binding. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows Phone Name Gets or sets the name of the binding. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows Phone Namespace Gets or sets the XML namespace of the binding. (Inherited from Binding.)
Public propertySupported by Silverlight for Windows Phone OpenTimeout Gets 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 Phone 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.)
Public propertySupported by Silverlight for Windows Phone Scheme Gets the URI scheme for transport used by the custom binding. (Overrides Binding.Scheme.)
Public propertySupported by Silverlight for Windows Phone 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
Public methodSupported by Silverlight for Windows Phone 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.)
Public methodSupported by Silverlight for Windows Phone 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.)
Public methodSupported by Silverlight for Windows Phone 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.)
Public methodSupported by Silverlight for Windows Phone 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.)
Public methodSupported by Silverlight for Windows Phone CreateBindingElements Returns a generic collection of the binding elements from the custom binding. (Overrides Binding.CreateBindingElements().)
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone 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.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetProperty<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 Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone 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:

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.