https://msdn.microsoft.com/en-us/library/ms731835.aspx

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 ,接口的代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace GettingStartedLib
{
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}

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.

最新文章

  1. C语言关于利用sscanf实现字符串相加减
  2. CentOS 配置防火墙操作实例(启、停、开、闭端口)
  3. ActiveReports 报表应用教程 (8)---交互式报表之动态过滤
  4. 让eclipse启动时拥有jre
  5. &lt;C Traps and Pitfalls&gt;笔记
  6. Java EXCEL导入的两种方式JXL和POI
  7. Hadoop InputFormat浅析
  8. 网站开发常用jQuery插件总结(七)背景插件backstretch
  9. [O] SQLite数据库报错:no such column
  10. 如何在github中贡献自己的力量
  11. php 使用操作符
  12. FJUT-这还是一道数论题
  13. 如何将一段文本编译成C#内存程序的过程
  14. Java设计模式学习记录-模板方法模式
  15. android build 编译打印详细过程
  16. C# 使用 HttpPost 请求调用 WebService
  17. Map的深浅拷贝的探究
  18. 51Nod:1134 最长递增子序列
  19. Spring框架(管理事务)
  20. java读取项目或包下面的属性文件方法

热门文章

  1. 两种js监听滚轮事件的方式
  2. Putty + Vim + Color
  3. Windows Phone中用到的类名及对应的命名控件及引用
  4. html常用单词和各种少见标签
  5. C# 链表操作
  6. Html5页面返回机制解决方案
  7. 之前可运行mongodb,后来却不行了显示Unclean shutdown detected mongodb
  8. SQL技术内幕四
  9. PHP学习心得(三)——处理表单
  10. 【8】了解Bootstrap栅格系统基础案例(3)