入门教程Getting Started Tutorial

本部分中包含的主题旨在帮助您快速了解到 Windows Communication Foundation (WCF) 编程体验。The topics contained in this section are intended to give you quick exposure to the Windows Communication Foundation (WCF) programming experience. 这些主题要根据本主题底部的列表中的顺序完成。They are designed to be completed in the order of the list at the bottom of this topic. 完成本教程,你初步了解创建 WCF 服务和客户端应用程序所需的步骤。Working through this tutorial gives you an introductory understanding of the steps required to create WCF service and client applications. 服务公开一个或多个终结点,其中每个终结点都公开一项或多项服务操作。A service exposes one or more endpoints, each of which exposes one or more service operations. 终结点服务的指定的地址,其中可以找到服务,一个绑定,它包含描述客户端必须如何与该服务,并定义的功能的协定进行通信的信息向其客户端提供服务。The endpoint of a service specifies an address where the service can be found, a binding that contains the information that describes how a client must communicate with the service, and a contract that defines the functionality provided by the service to its clients.

在完成本教程中的系列主题之后,您将会得到一个正在运行的服务,以及一个可以调用该服务的客户端。After you work through the sequence of topics in this tutorial, you will have a running service, and a client that calls the service. 前三个主题描述如何定义服务协定、如何实现服务协定以及如何承载服务。The first three topics describe how to define a service contract, how to implement the service contract, and how to host the service. 创建的服务自承载在控制台应用程序中。The service that is created is self-hosted within a console application. 服务还可在 Internet Information Services (IIS) 下承载。Services can also be hosted under Internet Information Services (IIS). 有关如何执行此操作的详细信息,请参阅如何: 承载在 IIS 中的 WCF 服务For more information about how to do this, see How to: Host a WCF Service in IIS. 在代码中配置服务;不过,也可以在配置文件中配置服务。The service is configured in code; however, services can also be configured within a configuration file. 有关使用配置文件的详细信息请参阅使用配置文件配置服务For more information about using a configuration file see Configuring Services Using Configuration Files.

后三个主题描述如何创建客户端代理,如何配置客户端应用程序,以及如何使用客户端代理调用由服务公开的操作。The next three topics describe how to create a client proxy, configure the client application, and use the client proxy to call service operation exposed by the service. 服务发布定义客户端应用程序与服务进行通信所需信息的元数据。Services publish metadata that define the information a client application needs to communicate with the service. Visual Studio 2012Visual Studio 2012 自动化访问此元数据的过程,并使用它来构建和配置服务的客户端应用程序。 automates the process of accessing this metadata and uses it to construct and configure the client application for the service. 如果你不使用Visual Studio 2012Visual Studio 2012,你可以使用ServiceModel 元数据实用工具 (Svcutil.exe)来构建和配置服务的客户端应用程序。If you are not using Visual Studio 2012Visual Studio 2012, you can use the ServiceModel Metadata Utility Tool (Svcutil.exe) to construct and configure the client application for the service.

本节中的所有主题均假定您使用 Visual Studio 2011 作为开发环境。All of the topics in this section assume you are using Visual Studio 2011 as the development environment. 如果你使用的其他开发环境,忽略的 Visual Studio 特定的说明进行操作。If you are using another development environment, ignore the Visual Studio specific instructions.

备注

如果你正在运行Windows VistaWindows Vista或更高版本的 Windows 操作系统中,你必须通过转到开始菜单和右键单击 Visual Studio 2011 并选择启动 Visual Studio以管理员身份运行If you are running Windows VistaWindows Vista or later versions of the Windows operating system, you must start Visual Studio by going to the Start menu and right clicking Visual Studio 2011 and selecting Run as Administrator. 若要始终以管理员身份可以创建一个快捷方式、 右键单击该快捷方式,选择属性,选择启动 Visual Studio 2011兼容性选项卡,然后选中管理员身份运行此程序复选框。To always launch Visual Studio 2011 as an administrator you can create a short cut, right click the short cut, select properties, select the Compatibility tab, and check the Run this program as an administrator checkbox. 在使用此快捷方式启动 Visual Studio 2011 时,会总是以管理员身份运行。When you start Visual Studio 2011 with this shortcut, it will always run as administrator.

有关示例应用程序可以下载到您的硬盘和运行,请参阅中的主题Windows Communication Foundation 示例For sample applications that can be downloaded to your hard disk and run, see the topics in Windows Communication Foundation Samples. 本主题,请参阅,特别是,入门For this topic, see, in particular, the Getting Started.

有关创建服务和客户端的更多详细信息,请参阅基本 WCF 编程For more in-depth information about creating services and clients, see Basic WCF Programming.

本节内容In This Section

如何:定义服务协定How to: Define a Service Contract
描述如何创建 WCF 协定使用用户定义的接口。Describes how to create a WCF contract using a user-defined interface. 协定定义服务所公开的功能。The contract defines the functionality exposed by the service.

如何:实现服务协定How to: Implement a Service Contract
描述如何实现服务协定。Describes how to implement a service contract. 一旦定义了某一协定后,必须使用某一服务类实现该协定。Once a contract is define, it must be implemented with a service class.

如何:托管和运行基本服务How to: Host and Run a Basic Service
描述如何在代码中配置服务的终结点,以及如何在控制台应用程序内承载服务。Describes how to configure an endpoint for the service in code and how to host the service in a console application. 若要激活服务,必须在运行时环境中配置和承载服务。To become active, a service must be configured and hosted within a run-time environment. 此环境将创建服务并控制其上下文和生存期。This environment creates the service and controls its context and lifetime.

如何:创建客户端How to: Create a Client
描述如何检索用于从 WCF 服务创建 WCF 客户端代理的元数据。Describes how to retrieve metadata used to create a WCF client proxy from a WCF service. 此进程使用 Visual Studio 2011 中的“添加服务引用”功能。This process uses the Add Service Reference functionality within Visual Studio 2011.

如何:配置客户端How to: Configure a Client
描述如何配置 WCF 客户端。配置客户端需要指定客户端用于访问服务的终结点。Describes how to configure a WCF client Configuring the client requires specifying the endpoint that the client uses to access the service.

如何:使用客户端How to: Use a Client
描述如何使用 WCF 客户端代理来调用服务操作。Describes how to use the WCF client proxy to invoke service operations.

参考Reference

ServiceContractAttribute

OperationContractAttribute

Windows Communication Foundation 示例Windows Communication Foundation Samples

基本编程生命周期Basic Programming Lifecycle

请参阅See Also

概念性概述Conceptual Overview
文档使用指南Guide to the Documentation
什么是 Windows Communication FoundationWhat Is Windows Communication Foundation
WCF 功能详细信息WCF Feature Details