How to: Define a Windows Communication Foundation Service Contract

This is the first of six tasks required to create a basic Windows Communication Foundation (WCF) application. For an overview of all six of the tasks, see the Getting Started Tutorial topic.

创建一个wcf应用需要6个步骤,本篇文章涉及的是第一个步骤。

When creating a WCF service, the first task is to define a service contract.

创建一个wcf服务的时候,第一个任务就是定义服务契约

The service contract specifies what operations the service supports.

服务契约规定了服务能够支持的操作

An operation can be thought of as a Web service method.

一个操作可以看做是一个web服务的方法

Contracts are created by defining a C++, C#, or Visual Basic (VB) interface.

契约是通过创建一个由C++,C#或VB定义的接口

Each method in the interface corresponds to a specific service operation.

接口中的每一个方法都对应到一个指定的服务操作

Each interface must have the ServiceContractAttribute applied to it and each operation must have the OperationContractAttribute attribute applied to it.

每一个接口必须应用service contract,每一个操作必须应用operation contract

If a method within an interface that has the ServiceContractAttribute attribute does not have the OperationContractAttribute attribute, that method is not exposed by the service.

如果有service contract属性的接口中的某一个方法没有operation contract属性,那么这个方法就不会被服务公开。

The code used for this task is provided in the example following the procedure.

用于这个任务的代码如下所示

To define a service contract   定义一个服务契约

  • 1.Open Visual Studio 2012 as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.

使用管理员权限打开VS2012

  • 2.Create a WCF Service Library project by clicking the File menu and selecting New, Project. In the New Project dialog, on the left-hand side of the dialog expand Visual C# for a C# project or Other Languagesand then Visual Basic for a Visual Basic project. Under the language selected select WCF and a list of project templates will be displayed on the center section of the dialog. Select WCF Service Library, and type GettingStartedLib in the Name textbox and GettingStarted in the Solution name textbox at the bottom of the dialog.

创建一个wcf服务的类库,在新建项目的对话框中,左边选择C#,然后选择wcf,然后选择wcf服务类库。项目名为GettingStartedLib ,解决方案名为GettingStarted

  • 3.Visual Studio will create the project which contains 3 files: IService1.cs (or IService1.vb), Service1.cs (or Service1.vb), and App.config. The IService1 file contains a default service contract. The Service1 file contains a default implementation of the service contract. The App.config file contains configuration needed to load the default service with the Visual Studio WCF Service Host. For more information about the WCF Service Host tool, see WCF Service Host (WcfSvcHost.exe)

VS会创建一个包含3个文件的项目:文件分别是IService1.cs,Service1.cs ,App.config。 IService1文件包含了默认的服务契约,Service1文件包含了实现了默认服务契约的代码。配置文件包含了加载默认服务所需要的配置

  • 4.Open the IService1.cs or IService1.vb file and delete the code within the namespace declaration leaving the namespace declaration. Inside the namespace declaration define a new interface called ICalculator as shown in the code below.

打开 IService1.cs 文件,删除命名空间内的代码,保留命名空间的声明。在命名空间中定义一个新的接口,接口名称为ICalculator ,接口的代码如下

This contract defines an online calculator. Notice the ICalculator interface is marked with the ServiceContractAttribute attribute. This attribute defines a namespace that is used to disambiguate the contract name. Each calculator operation is marked with the OperationContractAttribute attribute.

Note:

When using attributes to annotate an interface, member, or class, you can drop the "Attribute" part from the attribute name. So ServiceContractAttribute becomes [ServiceContract] in C#, or<ServiceContract> in Visual Basic.

时间: 2024-08-06 13:59:57

How to: Define a Windows Communication Foundation Service Contract的相关文章

How to: Implement a Windows Communication Foundation Service Contract

This is the second of six tasks required to create a basic Windows Communication Foundation (WCF) service and a client that can call the service. For an overview of all six tasks, see the Getting Started Tutorial topic. The next step in creating a WC

How to: Host and Run a Basic Windows Communication Foundation Service

This is the third of six tasks required to create a Windows Communication Foundation (WCF) application. For an overview of all six of the tasks, see the Getting Started Tutorial topic. This topic describes how to host a Windows Communication Foundati

概览 Windows Communication Foundation (WCF) 体系结构及其主要概念。代码示例演示 WCF 约定、终结点和行为

摘要:概览 Windows Communication Foundation (WCF) 体系结构及其主要概念.代码示例演示 WCF 约定.终结点和行为. 本页内容 简介 WCF 基础 代码示例 小结 简介 本文档提供 Windows Communication Foundation (WCF) 体系结构的概览.本文旨在阐释 WCF 中的主要概念以及它们如何协调工作.还有几个代码示例对这些概念进行深入阐释,但代码不是本文档的重点. 本文档下面的部分分为以下两个主要内容: WCF 基础:涵盖 WC

.NET Framework 4.6 and 4.5 &gt; 开发指南 &gt; 使用 WCF 的面向服务的应用程序 &gt; Windows Communication Foundation (WCF)

.NET Framework 4.6 and 4.5 > 开发指南 >  使用 WCF 的面向服务的应用程序 > Windows Communication Foundation (WCF) :https://msdn.microsoft.com/zh-cn/library/dd456779(v=vs.110).aspx 文档指南: https://msdn.microsoft.com/zh-cn/library/ms730846(v=vs.110).aspx 入门教程: https:/

MSDN 提供的有关 Windows Communication Foundation (WCF) 的各个方面的示例说明

有关 Windows Communication Foundation (WCF) 的各个方面的示例说明: https://www.microsoft.com/en-us/download/details.aspx?id=21459 或者:http://files.cnblogs.com/files/djdq/WF_WCF_Samples.part01.rar 和 http://files.cnblogs.com/files/djdq/WF_WCF_Samples.part02.rar 或者:(

Windows Communication Foundation (WCF)和Windows CardSpace的示例程序

微软公司昨天发布了一个Windows Communication Foundation (WCF)和Windows CardSpace的示例程序包,内容极为丰富,从最简单的Hello World到复杂的解决方案一应俱全.对于Windows Communication Foundation (WCF)和Windows CardSpace这两个新东西而言,这些示例程序无疑就是最好的学习资源. 下载地址:http://www.microsoft.com/downloads/details.aspx?F

Windows Communication Foundation(WCF)服务项目分类7756fgf

baozoumanhua.com/users/17321075/forum_articlesbaozoumanhua.com/users/17321075/followingsbaozoumanhua.com/users/17321075/talkingsbaozoumanhua.com/users/17321075/following_articlesbaozoumanhua.com/users/17321075/articlesbaozoumanhua.com/users/17321105b

Windows Workflow Foundation技术介绍(基于.NET Framework 4.5)

Windows Workflow Foundation技术介绍(基于.NET Framework 4.5) 转自:http://www.cpiso.cn/jsyj/ghxx/2014/5/15/459.shtml Microsoft Windows Workflow Foundation (WF) 是一个可扩展框架,用于在 Windows 平台上开发工作流解决方案.Windows Workflow Foundation 同时提供了 API 和一些工具,用于开发和执行基于工作流的应用程序.Wind

为应用程序池“XX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误

场景 WCF应用程序部署在IIS7中,使用net.tcp协议对外给几百台客户端提供服务,应用程序池不断崩溃重启. 分析过程 在事件查看器中看到的错误信息类似于 为应用程序池"XX"提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误 使用windbg在崩溃时进行分析,找到引发崩溃的堆栈的最顶部信息进行搜索,找到两个补丁包安装上 用于搜索的顶部异常信息为:IOCompletionThunk.UnhandledExceptionFra