什么是 Windows Communication FoundationWhat Is Windows Communication Foundation

Windows Communication Foundation (WCF)是用于生成面向服务的应用程序的框架。Windows Communication Foundation (WCF) is a framework for building service-oriented applications. 使用 WCF,可以将数据作为异步消息从一个服务终结点发送到另一个服务终结点。Using WCF, you can send data as asynchronous messages from one service endpoint to another. 服务终结点可以是由 IIS 承载的持续可用的服务的一部分,也可以是应用程序中承载的服务。A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. 终结点可以是从服务终结点请求数据的服务客户端。An endpoint can be a client of a service that requests data from a service endpoint. 简单消息可以是作为 XML 发送的单个字符或单个单词,复杂消息可以是二进制数据流。The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. 一些示例方案包括:A few sample scenarios include:

  • 处理企业事务的安全服务。A secure service to process business transactions.

  • 将当前数据提供给其他服务(例如流量报告或其他监视服务)的服务。A service that supplies current data to others, such as a traffic report or other monitoring service.

  • 使两个人能够实时通信或交换数据的聊天服务。A chat service that allows two people to communicate or exchange data in real time.

  • 轮询一个或多个服务以查找数据并将其以逻辑表现形式展示出来的面板应用程序。A dashboard application that polls one or more services for data and presents it in a logical presentation.

  • 将使用 Windows Workflow Foundation 实现的工作流作为 WCF 服务公开。Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.

  • 轮询服务以查找最新数据源的 Silverlight 应用程序。A Silverlight application to poll a service for the latest data feeds.

虽然在存在 WCF 之前可以创建此类应用程序,但 WCF 使终结点的开发比以往更容易。While creating such applications was possible prior to the existence of WCF, WCF makes the development of endpoints easier than ever. 总而言之,WCF 旨在提供一种可管理的方法,可用于创建 Web 服务和 Web 服务客户端。In summary, WCF is designed to offer a manageable approach to creating Web services and Web service clients.

WCF 的功能Features of WCF

WCF 包括以下功能集。WCF includes the following set of features. 有关详细信息,请参阅WCF 功能详细信息For more information, see WCF Feature Details.

  • 服务导向Service Orientation

    使用 WS 标准的一个结果是 WCF 使你能够创建面向服务的应用程序。One consequence of using WS standards is that WCF enables you to create service oriented applications. 面向服务的体系结构 (SOA) 依赖 Web 服务发送和接收数据。Service-oriented architecture (SOA) is the reliance on Web services to send and receive data. 这些服务具有松耦合的常规优点,而不是从一个应用程序到另一个应用程序进行硬编码。The services have the general advantage of being loosely-coupled instead of hard-coded from one application to another. 松耦合关系意味着只要符合基本协定,则在任何平台上创建的任何客户端均可连接到所有服务。A loosely-coupled relationship implies that any client created on any platform can connect to any service as long as the essential contracts are met.

  • 互操作性Interoperability

    WCF 实现了用于 Web 服务互操作性的现代行业标准。WCF implements modern industry standards for Web service interoperability. 有关支持的标准的详细信息,请参阅互操作性和集成For more information about the supported standards, see Interoperability and Integration.

  • 多种消息模式Multiple Message Patterns

    采用多种模式之一交换消息。Messages are exchanged in one of several patterns. 最常用的模式是请求/答复模式,其中一个终结点从另一个终结点请求数据,The most common pattern is the request/reply pattern, where one endpoint requests data from a second endpoint. 另一个终结点进行答复。The second endpoint replies. 还有其他模式,比如单向消息,其中只有一个终结点发送消息,而不期望得到答复。There are other patterns such as a one-way message in which a single endpoint sends a message without any expectation of a reply. 更复杂的模式是双工交换模式,在该模式下,两个终结点建立连接并来回发送数据,类似于即时消息传递程序。A more complex pattern is the duplex exchange pattern where two endpoints establish a connection and send data back and forth, similar to an instant messaging program. 有关如何使用 WCF 实现不同消息交换模式的详细信息,请参阅约定For more information about how to implement different message exchange patterns using WCF see Contracts.

  • 服务元数据Service Metadata

    WCF 支持使用行业标准(如 WSDL、XML 架构和 WS 策略)中指定的格式发布服务元数据。WCF supports publishing service metadata using formats specified in industry standards such as WSDL, XML Schema and WS-Policy. 此元数据可用于自动生成和配置用于访问 WCF 服务的客户端。This metadata can be used to automatically generate and configure clients for accessing WCF services. 可通过 HTTP 和 HTTPS 来发布元数据,也可使用 Web 服务元数据交换标准来发布元数据。Metadata can be published over HTTP and HTTPS or using the Web Service Metadata Exchange standard. 有关详细信息,请参阅元数据For more information, see Metadata.

  • 数据协定Data Contracts

    因为 WCF 是使用 .NET Framework 生成的,所以它还包含提供要强制执行的协定的代码友好方法。Because WCF is built using the .NET Framework, it also includes code-friendly methods of supplying the contracts you want to enforce. 数据协定就是其中一种通用类型的协定。One of the universal types of contracts is the data contract. 实质上,当您使用 Visual C# 或 Visual Basic 对服务进行编码时,处理数据的最简单方法是使用属于数据实体的属性创建表示该数据实体的类。In essence, as you code your service using Visual C# or Visual Basic, the easiest way to handle data is by creating classes that represent a data entity with properties that belong to the data entity. WCF 包含一个用于以这种简单方式处理数据的综合系统。WCF includes a comprehensive system for working with data in this easy manner. 在创建了表示数据的类之后,服务会自动生成使客户端能够符合所设计数据类型的元数据。Once you have created the classes that represent data, your service automatically generates the metadata that allows clients to comply with the data types you have designed. 有关详细信息,请参阅使用数据协定For more information, see Using Data Contracts.

  • SecuritySecurity

    可对消息进加密以保护隐私,而且可以要求用户对其自身进行身份验证,然后才允许接收消息。Messages can be encrypted to protect privacy and you can require users to authenticate themselves before being allowed to receive messages. 可使用众所周知的标准(如 SSL 或 WS-SecureConversation)实现安全性。Security can be implemented using well-known standards such as SSL or WS-SecureConversation. 有关详细信息,请参阅安全性For more information, see Security.

  • 多种传输和编码方式Multiple Transports and Encodings

    可通过多种内置传输协议和编码中的任意一种发送消息。Messages can be sent on any of several built-in transport protocols and encodings. 最常见的协议和编码是使用超文本传输协议(HTTP)发送文本编码的 SOAP 消息,以便在万维网上使用。The most common protocol and encoding is to send text encoded SOAP messages using the HyperText Transfer Protocol (HTTP) for use on the World Wide Web. 另外,WCF 还允许通过 TCP、命名管道或 MSMQ 发送消息。Alternatively, WCF allows you to send messages over TCP, named pipes, or MSMQ. 这些消息可以编码为文本,也可以使用优化的二进制格式。These messages can be encoded as text or using an optimized binary format. 使用 MTOM 标准可有效地发送二进制数据。Binary data can be sent efficiently using the MTOM standard. 如果所提供的传输或编码都不符合您的需要,您可以创建自己的自定义传输或编码。If none of the provided transports or encodings suit your needs you can create your own custom transport or encoding. 有关 WCF 支持的传输和编码的详细信息,请参阅传输For more information about transports and encodings supported by WCF see Transports.

  • 可靠的排队消息Reliable and Queued Messages

    WCF 支持可靠的消息交换,其中使用通过 WS-TRUST 消息和使用 MSMQ 实现的可靠会话。WCF supports reliable message exchange using reliable sessions implemented over WS-Reliable Messaging and using MSMQ. 有关 WCF 中可靠和排队消息支持的详细信息,请参阅队列和可靠会话For more information about reliable and queued messaging support in WCF see Queues and Reliable Sessions.

  • 持久性消息Durable Messages

    持久性消息决不会由于通信中断而丢失。A durable message is one that is never lost due to a disruption in the communication. 持久性消息模式的消息会始终保存到数据库中。The messages in a durable message pattern are always saved to a database. 如果发生中断,数据库将允许您在恢复连接后恢复消息交换。If a disruption occurs, the database allows you to resume the message exchange when the connection is restored. 你还可以使用 Windows Workflow Foundation (WF)创建持久消息。You can also create a durable message using the Windows Workflow Foundation (WF). 有关详细信息,请参阅工作流服务For more information, see Workflow Services.

  • 事务Transactions

    WCF 还支持使用以下三种事务模型之一的事务: AtomicTransactions、System.Transactions 命名空间中的 Api 和 Microsoft 分布式事务处理协调器。WCF also supports transactions using one of three transaction models: WS-AtomicTransactions, the APIs in the System.Transactions namespace, and Microsoft Distributed Transaction Coordinator. 有关 WCF 中事务支持的详细信息,请参阅事务For more information about transaction support in WCF see Transactions.

  • AJAX 和 REST 支持AJAX and REST Support

    REST 是不断发展的 Web 2.0 技术的一个示例。REST is an example of an evolving Web 2.0 technology. 可以将 WCF 配置为处理未包装在 SOAP 信封中的 "纯文本" XML 数据。WCF can be configured to process "plain" XML data that is not wrapped in a SOAP envelope. WCF 还可以扩展以支持特定的 XML 格式,如 ATOM (流行的 RSS 标准),甚至非 XML 格式,例如 JavaScript 对象表示法(JSON)。WCF can also be extended to support specific XML formats, such as ATOM (a popular RSS standard), and even non-XML formats, such as JavaScript Object Notation (JSON).

  • 扩展性Extensibility

    WCF 体系结构具有大量扩展点。The WCF architecture has a number of extensibility points. 如果需要额外功能,它还提供许多入口点,允许您自定义服务的行为。If extra capability is required, there are a number of entry points that allow you to customize the behavior of a service. 有关可用扩展点的详细信息,请参阅扩展 WCFFor more information about available extensibility points see Extending WCF.

WCF 与其他 Microsoft 技术的集成WCF Integration with Other Microsoft Technologies

WCF 是一个灵活的平台。WCF is a flexible platform. 由于这种极大的灵活性,还在其他一些 Microsoft 产品中使用 WCF。Because of this extreme flexibility, WCF is also used in several other Microsoft products. 通过了解 WCF 的基础知识,你还可以直接使用这些产品中的任何一个。By understanding the basics of WCF, you have an immediate advantage if you also use any of these products.

与 WCF 配对的第一种技术是 Windows Workflow Foundation (WF)。The first technology to pair with WCF was the Windows Workflow Foundation (WF). 工作流通过将工作流中的步骤封装为 "活动" 来简化应用程序开发。Workflows simplify application development by encapsulating steps in the workflow as "activities." 在 Windows Workflow Foundation 的第一版中,开发人员必须为工作流创建主机。In the first version of Windows Workflow Foundation, a developer had to create a host for the workflow. Windows Workflow Foundation 的下一个版本与 WCF 集成。The next version of Windows Workflow Foundation was integrated with WCF. 这允许在 WCF 服务中轻松承载任何工作流。That allowed any workflow to be easily hosted in a WCF service. 可以通过在 Visual Studio 2012 或更高版本中自动选择 WF/WCF 项目类型来实现此目的。You can do this by automatically choosing the WF/WCF project type in Visual Studio 2012 or later.

Microsoft BizTalk Server R2 也使用 WCF 作为通信技术。Microsoft BizTalk Server R2 also utilizes WCF as a communication technology. BizTalk 设计用于接收一种标准化格式的数据,然后将其转换为另一种格式。BizTalk is designed to receive and transform data from one standardized format to another. 必须将消息传递到其中央消息框,在这里可以使用严格的映射,也可以通过使用其中一种 BizTalk 功能(如其工作流引擎)来转换消息。Messages must be delivered to its central message box where the message can be transformed using either a strict mapping or by using one of the BizTalk features such as its workflow engine. BizTalk 现在可以使用 WCF 业务线(LOB)适配器将消息传递到消息框。BizTalk can now use the WCF Line of Business (LOB) adapter to deliver messages to the message box.

Microsoft Silverlight 是一个用于创建可互操作的、丰富 Web 应用程序的平台,允许开发人员创建媒体密集的网站(如流视频)。Microsoft Silverlight is a platform for creating interoperable, rich Web applications that allow developers to create media-intensive Web sites (such as streaming video). 从版本2开始,Silverlight 合并了 WCF 作为通信技术,以将 Silverlight 应用程序连接到 WCF 终结点。Beginning with version 2, Silverlight has incorporated WCF as a communication technology to connect Silverlight applications to WCF endpoints.

Windows Server AppFabric 应用程序服务器的托管功能专门用于部署和管理使用 WCF 进行通信的应用程序。The hosting features of Windows Server AppFabric application server are specifically designed for deploying and managing applications that use WCF for communication. 托管功能包括丰富的工具和配置选项,专门为启用了 WCF 的应用程序而设计。The hosting features include rich tooling and configuration options specifically designed for WCF-enabled applications.

请参阅See also