Binding Binding Binding Binding Class

定义

包含绑定元素,这些元素指定客户端和服务之间的通信所用的协议、传输和消息编码器。Contains the binding elements that specify the protocols, transports, and message encoders used for communication between clients and services.

public ref class Binding abstract : System::ServiceModel::IDefaultCommunicationTimeouts
public abstract class Binding : System.ServiceModel.IDefaultCommunicationTimeouts
type Binding = class
    interface IDefaultCommunicationTimeouts
Public MustInherit Class Binding
Implements IDefaultCommunicationTimeouts
继承
BindingBindingBindingBinding
派生
实现

示例

BasicHttpBinding binding = new BasicHttpBinding();

binding.Name = "binding1";

Uri baseAddress = new Uri("http://localhost:8000/servicemodelsamples/service");
Uri address = new Uri("http://localhost:8000/servicemodelsamples/service/calc");

// Create a ServiceHost for the CalculatorService type and provide the base address.
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
{
    serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, address);

    // 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();
}

注解

表示一个绑定元素集合,其中每一个元素都描述一个终结点与其他终结点的通信方式的一个方面,这些元素一致地内置到客户端上的通道工厂和服务上的通道侦听器中。Represents a collection of binding elements, each of which describes an aspect of how an endpoint communicates with other endpoints and that are built, consistently, into a channel factory on the client and into a channel listener on the service. 绑定包含绑定元素集合,这些元素与协议通道、传输通道和消息编码器相对应。A binding contains a collection of binding elements that correspond to protocol channels, transport channels, and message encoders. 协议通道可以有任意数目的绑定元素,但是每个传输通道和消息编码器都有且只有一个绑定元素。There can be any number of binding elements for protocol channels but one and only one binding element for each the transport and message encoder. 一个绑定中通常有六层绑定元素。There are commonly six layers of binding elements in a binding. 只有堆栈底部的传输和编码绑定元素是必需的。Only the transport and encoding binding elements at the bottom of the stack are required. 因为如果未指定的编码,都需要为每个绑定,一个编码,Windows Communication Foundation (WCF) 将添加一个默认编码。Because an encoding is required for each binding, if an encoding is not specified, Windows Communication Foundation (WCF) adds a default encoding for you. 对于 HTTP 和 HTTPS 传输,默认编码是 Text/XML,对于其他传输,默认编码为二进制编码。The default is Text/XML for the HTTP and HTTPS transports, and Binary for other transports.

下表总结了每层的选项。The following table summarizes the options for each layer.

Layer 选项Options 必需Required
事务流Transaction Flow TransactionFlowBindingElement NoNo
可靠性Reliability ReliableSessionBindingElement NoNo
安全性Security 对称、非对称、传输级Symmetric, Asymmetric, Transport-Level NoNo
形状更改Shape Change CompositeDuplexBindingElement NoNo
传输升级Transport Upgrades SSL 流、Windows 流、对等解析程序SSL stream, Windows stream, Peer Resolver NoNo
编码Encoding 文本、二进制、MTOM、自定义Text, Binary, MTOM, Custom Yes
传输Transport TCP、命名管道、HTTP、HTTPS、MSMQ、自定义TCP, Named Pipes, HTTP, HTTPS, MSMQ, Custom Yes

每个绑定元素都提供在客户端上生成通道工厂和在服务上生成通道侦听器的规范。Each binding element provides the specification for building a channel factory on the client and a channel listener on the service. 例如,在构建通道工厂堆栈时,对于绑定中的每个绑定元素,堆栈中都有一个通道工厂。When the channel factory stack is constructed, for example, there is one channel factory in the stack for each binding element in the binding. 在服务上,堆栈中的通道侦听器也具有同样的对应关系。The same sort of mapping applies to the channel listeners in the stack on the service. 客户端和服务上的一致性对于在这些终结点之间建立基于通道的连接非常重要。Consistency on the client and service is critical for establishing the channel-based connection between these endpoints. 这样,每个工厂和侦听器才能处理通道堆栈中与之连接的相应通道的发送和接收操作,然后,这些通道才能发送和接收用于通信的消息。Each factory and listener, in turn, processes the sending and accepting of the corresponding channels in the channel stack that connects them, and these channels can then send and receive the messages used to communicate.

每个 Binding 实例都具有 NameNamespace,它们结合起来在服务的元数据中唯一标识每个实例。Each instance of a Binding has a Name and Namespace that together uniquely identify it in the metadata of the service. 如果不指定任何名称或命名空间,WCF 将为您添加默认值。If no name or namespace is specified, WCF adds a default values for you. 默认名称是null,默认命名空间为http://tempuri.org/。The default name is null and the default namespace is http://tempuri.org/. 绑定的这一用户名不同于协议名称的规范,后者由 Scheme 属性指定。This user name for the binding is distinct from the specification of the protocol name, which is specified by the Scheme property. 例如,如果要添加更多 HTTP 绑定,可以将它们命名为任何名称,并可将其所有架构都设置为“http”。If you want to add more HTTP bindings, for example, you can name them whatever you want and set all of their schemes to "http". 不存在基于 Scheme 的固有应用程序或计算机调度。There is no inherent application or machine dispatch based on the Scheme. 这样,就避免了一个常见问题,即无法为已知协议注册附加处理程序。So you avoid the common problem of being unable to register additional handlers for well-known protocols. 通过给每个版本提供不同的名称,还可以轻松地并行使用一个绑定的多个版本。You can also easily work with multiple versions of a binding side-by-side by giving each version a different name.

Binding 类实现 IDefaultCommunicationTimeouts 接口,从而减少通过长时间占用资源来实施的拒绝服务 (DOS) 攻击。The Binding class implements the IDefaultCommunicationTimeouts interface to mitigate Denial of Service (DOS) attacks that rely on tying up resources for extended intervals of time. 该实现指定打开和关闭连接的通信超时值,以及与接收和发送消息相关联的读写操作的通信超时值。The implementation specifies the communication timeouts values for opening and closing connections and for the reading and writing operations associated with receiving and sending messages. 用于获取和设置这些超时值的属性及其默认值操作总结在下表中。The properties used to get and set these timeouts and their default values operations are summarized in the following table.

超时属性Timeout Property 默认值Default Value
OpenTimeout 1 分钟1 minute
CloseTimeout 1 分钟1 minute
SendTimeout 1 分钟1 minute
ReceiveTimeout 10 分钟10 minutes

通过继承 Binding 创建绑定时,必须重写 CreateBindingElementsWhen you create a binding by inheriting from Binding, you must override CreateBindingElements.

此外,还可以定义自己的绑定元素,并将它们插入到前面表中定义的任何层之间。In addition, you can define your own binding elements and insert them between any of the defined layers in the preceding table. 有关更多信息,请参见 CustomBinding 类。For more information, see the CustomBinding class.

构造函数

Binding() Binding() Binding() Binding()

使用默认的名称和命名空间初始化 Binding 类的新实例。Initializes a new instance of the Binding class with a default name and namespace.

Binding(String, String) Binding(String, String) Binding(String, String) Binding(String, String)

使用指定的服务绑定初始化 Binding 类的新实例。Initializes a new instance of the Binding class from a specified binding of the service.

属性

CloseTimeout CloseTimeout CloseTimeout CloseTimeout

获取或设置在传输引发异常之前可用于关闭连接的时间间隔。Gets or sets the interval of time provided for a connection to close before the transport raises an exception.

MessageVersion MessageVersion MessageVersion MessageVersion

获取由绑定所配置的客户端和服务使用的消息版本。Gets the message version used by clients and services configured with the binding.

Name Name Name Name

获取或设置绑定的名称。Gets or sets the name of the binding.

Namespace Namespace Namespace Namespace

获取或设置绑定的 XML 命名空间。Gets or sets the XML namespace of the binding.

OpenTimeout OpenTimeout OpenTimeout OpenTimeout

获取或设置在传输引发异常之前可用于打开连接的时间间隔。Gets or sets the interval of time provided for a connection to open before the transport raises an exception.

ReceiveTimeout ReceiveTimeout ReceiveTimeout 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.

Scheme Scheme Scheme Scheme

在派生类中实现时,设置 URI 架构,此架构指定由绑定生成的通道和侦听器工厂所使用的传输。When implemented in a derived class, sets the URI scheme that specifies the transport used by the channel and listener factories that are built by the bindings.

SendTimeout SendTimeout SendTimeout SendTimeout

获取或设置在传输引发异常之前可用于完成写入操作的时间间隔。Gets or sets the interval of time provided for a write operation to complete before the transport raises an exception.

方法

BuildChannelFactory<TChannel>(BindingParameterCollection) BuildChannelFactory<TChannel>(BindingParameterCollection) BuildChannelFactory<TChannel>(BindingParameterCollection) 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.

BuildChannelFactory<TChannel>(Object[]) BuildChannelFactory<TChannel>(Object[]) BuildChannelFactory<TChannel>(Object[]) 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.

BuildChannelListener<TChannel>(BindingParameterCollection) BuildChannelListener<TChannel>(BindingParameterCollection) BuildChannelListener<TChannel>(BindingParameterCollection) BuildChannelListener<TChannel>(BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足绑定参数集合所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified by a collection of binding parameters.

BuildChannelListener<TChannel>(Object[]) BuildChannelListener<TChannel>(Object[]) BuildChannelListener<TChannel>(Object[]) BuildChannelListener<TChannel>(Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, Object[]) BuildChannelListener<TChannel>(Uri, Object[]) BuildChannelListener<TChannel>(Uri, Object[]) BuildChannelListener<TChannel>(Uri, Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, String, BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection) BuildChannelListener<TChannel>(Uri, String, ListenUriMode, BindingParameterCollection)

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[]) BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[]) BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[]) BuildChannelListener<TChannel>(Uri, String, ListenUriMode, Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

BuildChannelListener<TChannel>(Uri, String, Object[]) BuildChannelListener<TChannel>(Uri, String, Object[]) BuildChannelListener<TChannel>(Uri, String, Object[]) BuildChannelListener<TChannel>(Uri, String, Object[])

在服务上生成通道侦听器,该通道侦听器接受具有指定类型的通道并满足所指定的功能。Builds the channel listener on the service that accepts a specified type of channel and that satisfies the features specified.

CanBuildChannelFactory<TChannel>(BindingParameterCollection) CanBuildChannelFactory<TChannel>(BindingParameterCollection) CanBuildChannelFactory<TChannel>(BindingParameterCollection) 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.

CanBuildChannelFactory<TChannel>(Object[]) CanBuildChannelFactory<TChannel>(Object[]) CanBuildChannelFactory<TChannel>(Object[]) 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.

CanBuildChannelListener<TChannel>(BindingParameterCollection) CanBuildChannelListener<TChannel>(BindingParameterCollection) CanBuildChannelListener<TChannel>(BindingParameterCollection) CanBuildChannelListener<TChannel>(BindingParameterCollection)

返回一个值,该值指示当前绑定是否可以在服务上生成满足指定绑定参数集合的通道侦听器堆栈。Returns a value that indicates whether the current binding can build a channel listener stack on the service that satisfies the collection of binding parameters specified.

CanBuildChannelListener<TChannel>(Object[]) CanBuildChannelListener<TChannel>(Object[]) CanBuildChannelListener<TChannel>(Object[]) CanBuildChannelListener<TChannel>(Object[])

返回一个值,该值指示当前绑定是否可以在服务上生成满足对象数组所指定的条件的通道侦听器堆栈。Returns a value that indicates whether the current binding can build a channel listener stack on the service that satisfies the criteria specified in an array of objects.

CreateBindingElements() CreateBindingElements() CreateBindingElements() CreateBindingElements()

在派生类中重写时,创建一个集合,该集合包含属于当前绑定的绑定元素。When overridden in a derived class, creates a collection that contains the binding elements that are part of the current binding.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetProperty<T>(BindingParameterCollection) GetProperty<T>(BindingParameterCollection) GetProperty<T>(BindingParameterCollection) GetProperty<T>(BindingParameterCollection)

从绑定堆栈的适当层返回所请求的类型化对象(如果存在)。Returns a typed object requested, if present, from the appropriate layer in the binding stack.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ShouldSerializeName() ShouldSerializeName() ShouldSerializeName() ShouldSerializeName()

返回是否应序列化绑定名称。Returns whether the name of the binding should be serialized.

ShouldSerializeNamespace() ShouldSerializeNamespace() ShouldSerializeNamespace() ShouldSerializeNamespace()

返回是否应序列化绑定命名空间。Returns whether the namespace of the binding should be serialized.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于