Windows Communication Foundation 绑定概述Windows Communication Foundation Bindings Overview

绑定是用于指定连接到 Windows Communication Foundation (WCF)服务的终结点所需的通信详细信息的对象。Bindings are objects that are used to specify the communication details that are required to connect to the endpoint of a Windows Communication Foundation (WCF) service. WCF 服务中的每个终结点都需要一个明确指定的绑定。Each endpoint in a WCF service requires a binding to be well-specified. 本主题概述绑定定义的通信详细信息的类型、绑定的元素、WCF 中包含的绑定以及如何为终结点指定绑定。This topic outlines the types of communication details that the bindings define, the elements of a binding, what bindings are included in WCF, and how a binding can be specified for an endpoint.

绑定所定义的内容What a Binding Defines

绑定中的信息可能非常基本,也可能非常复杂。The information in a binding can be very basic, or very complex. 最基本的绑定仅指定连接终结点所必需的传输协议(如 HTTP)。The most basic binding specifies only the transport protocol (such as HTTP) that must be used to connect to the endpoint. 一般来说,绑定包含的有关如何连接到终结点的信息属于以下类别之一:More generally, the information a binding contains about how to connect to an endpoint falls into one of the following categories:

通讯Protocols
确定要使用的安全机制:可靠消息传递功能或事务上下文流设置。Determines the security mechanism being used: either reliable messaging capability or transaction context flow settings.

编码器Encoding
确定消息编码(例如,文本或二进制)。Determines the message encoding (for example, text or binary).

TransportTransport
确定要使用的基础传输协议(例如,TCP 或 HTTP)。Determines the underlying transport protocol to use (for example, TCP or HTTP).

绑定的元素The Elements of a Binding

绑定基本上由绑定元素的有序堆栈组成,其中每个元素都指定了连接到服务终结点所必需的通信信息的一部分。A binding basically consists of an ordered stack of binding elements, each of which specifies part of the communication information required to connect to a service endpoint. 该堆栈中最底下的两层都是必需的。The two lowest layers in the stack are both required. 该堆栈的最底层为传输绑定元素,而紧挨着它的上面一层为包含消息编码规范的元素。At the base of the stack is the transport binding element and just above this is the element that contains the message encoding specifications. 用于指定其他通信协议的可选绑定元素位于这两个必需元素的上面。The optional binding elements that specify the other communication protocols are layered above these two required elements. 有关这些绑定元素及其正确排序的详细信息,请参阅自定义绑定For more information about these binding elements and their correct ordering, see Custom Bindings.

系统提供的绑定System-Provided Bindings

绑定中的信息可能十分复杂,而且某些设置可能与其他设置不兼容。The information in a binding can be complex, and some settings may not be compatible with others. 出于此原因,WCF 包含一组系统提供的绑定。For this reason, WCF includes a set of system-provided bindings. 这些绑定旨在满足大多数应用程序需求。These bindings are designed to cover most application requirements. 下面的类表示系统提供的绑定的一些示例:The following classes represent some examples of system-provided bindings:

  • BasicHttpBinding:一个 HTTP 协议绑定,适用于连接到符合 WS-I 基本配置文件规范的 Web 服务(例如,基于 ASP.NET Web 服务的服务)。BasicHttpBinding: An HTTP protocol binding suitable for connecting to Web services that conforms to the WS-I Basic Profile specification (for example, ASP.NET Web services-based services).

  • WSHttpBinding:一个可互操作的绑定,适用于连接到符合 WS-* 协议的终结点。WSHttpBinding: An interoperable binding suitable for connecting to endpoints that conform to the WS-* protocols.

  • NetNamedPipeBinding:使用 .NET Framework 连接到同一计算机上的其他 WCF 终结点。NetNamedPipeBinding: Uses the .NET Framework to connect to other WCF endpoints on the same machine.

  • NetMsmqBinding:使用 .NET Framework 创建与其他 WCF 终结点的排队消息连接。NetMsmqBinding: Uses the .NET Framework to create queued message connections with other WCF endpoints.

  • NetTcpBinding:此绑定提供比 HTTP 绑定更高的性能,非常适合在本地网络中使用。NetTcpBinding: This binding offers higher performance than HTTP bindings and is ideal for use in a local network.

有关 WCF 提供的所有绑定的完整列表,请参阅系统提供的绑定For a complete list, with descriptions, of all the WCF-provided bindings, see System-Provided Bindings.

使用自己的绑定Using Your Own Bindings

如果系统提供的绑定都不具有服务应用程序所需的正确功能组合,则可以创建自己的绑定。If none of the system-provided bindings included has the right combination of features that a service application requires, you can create your own binding. 有两种方法可以实现此目的。There are two ways to do this. 您可以使用 CustomBinding 对象从预先存在的绑定元素创建新的绑定,也可以通过从 Binding 绑定派生来创建完全由用户定义的绑定。You can either create a new binding from pre-existing binding elements using a CustomBinding object or you can create a completely user-defined binding by deriving from the Binding binding. 有关使用这两种方法创建自己的绑定的详细信息,请参阅自定义绑定创建用户定义的绑定For more information about creating your own binding using these two approaches, see Custom Bindings and Creating User-Defined Bindings.

使用绑定Using Bindings

使用绑定需要执行两个基本步骤:Using bindings entails two basic steps:

  1. 选择或定义绑定。Select or define a binding. 最简单的方法是选择 WCF 中包含的系统提供的绑定之一,并将其与默认设置一起使用。The easiest method is to choose one of the system-provided bindings included with WCF and use it with its default settings. 您还可以选择一个系统提供的绑定,然后根据您的要求重新设置它的属性值。You can also choose a system-provided binding and reset its property values to suit your requirements. 或者,你可以创建一个自定义绑定或用户定义的绑定,以实现更高程度的控制和自定义。Alternatively, you can create a custom binding or a user-defined binding to have higher degrees of control and customization.

  2. 创建一个使用所选择或定义的绑定的终结点。Create an endpoint that uses the binding selected or defined.

代码和配置Code and Configuration

可以通过两种方式来定义绑定:通过代码或通过配置。You can define bindings in two ways: through code or through configuration. 这两种方法与你使用的是系统提供的绑定还是自定义绑定无关。These two approaches do not depend on whether you are using a system-provided binding or a custom binding. 通常,使用代码可以使你在设计时对绑定的定义拥有完全的控制。In general, using code gives you complete control over the definition of a binding at design time. 另一方面,使用配置允许系统管理员或 WCF 服务或客户端的用户更改绑定的参数,而无需重新编译服务应用程序。Using configuration, on the other hand, allows a system administrator or the user of a WCF service or client to change the parameters of a binding without having to recompile the service application. 这种灵活性通常是必需的,因为无法预测要在其上部署 WCF 应用程序的特定计算机要求。This flexibility is often desirable because there is no way to predict specific machine requirements on which a WCF application is to be deployed. 通过将绑定(和寻址)信息保持在代码外部,人们可以更改这些信息,而不必重新编译或重新部署应用程序。Keeping the binding (and the addressing) information out of the code allows them to change without requiring recompilation or redeployment of the application. 请注意,代码中定义的绑定是在配置中指定的绑定之后创建的,这使得代码定义的绑定可以覆盖配置中定义的任何绑定。Note that bindings defined in code are created after bindings specified in configuration, allowing the code-defined bindings to overwrite any configuration-defined bindings.

请参阅See also