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.

Step1 - How to: Define a Windows Communication Foundation Service Contract的更多相关文章

  1. 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) app ...

  2. 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) se ...

  3. Step2 - 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) se ...

  4. 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) applicati ...

  5. Step3 - 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) applicati ...

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

    微软公司昨天发布了一个Windows Communication Foundation (WCF)和Windows CardSpace的示例程序包,内容极为丰富,从最简单的Hello World到复杂 ...

  7. How to: Create a Windows Communication Foundation Client

    How to: Create a Windows Communication Foundation Client To create a Windows Communication Foundatio ...

  8. 【翻译习作】 Windows Workflow Foundation程序开发-第一章02

    1.2      Windows Workflow概览 微软的Windows Workflow Foundation(简称WF)是.NET框架3.0版的一部分..NET3.0其它主要部分是Window ...

  9. Workflow-Microsoft:Windows Workflow Foundation

    ylbtech-Workflow-Microsoft:Windows Workflow Foundation 1. Windows Workflow Foundation返回顶部 1.1. Windo ...

随机推荐

  1. js模块化编程之CommonJS和AMD/CMD!

    有了模块,我们就可以更方便地使用别人的代码,想要什么功能,就加载什么模块. 但是,这样做有一个前提,那就是大家必须以同样的方式编写模块,否则你有你的写法,我有我的写法,岂不是乱了套! 于是下面三个模块 ...

  2. Spring MVC @RequestBody自动转JSON HTTP415错误解决方法

    转自:http://blog.csdn.net/tiantiandjava/article/details/46125141 项目中想用@RequestBody直接接收json串转成对象 网上查了使用 ...

  3. Codeforces 601B(贪心+斜率+组合数学+单调栈)

    题面 传送门 题目大意: L(h)的值是区间[L,R]内,abs(h[i]-h[j])/(i-j)的最大值.现在有q个询问,每个询问表示询问区间[L,R]内,所有子序列的L(h)的值的和 分析 将|h ...

  4. js页面加载的几种方式的速度: window.onload、 $(window).load、 $(function(){})、 $(document).ready(function () {})、onload=‘asd()’

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. POJ-3468 A Simple Problem with Integers (区间求和,成段加减)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  6. 二分查找---有序数组的 Single Element

    有序数组的 Single Element 540. Single Element in a Sorted Array (Medium) Input: [1, 1, 2, 3, 3, 4, 4, 8, ...

  7. ASP.NET @URL帮助类

    原文: https://www.cnblogs.com/bobo-show/p/5746389.html    https://www.cnblogs.com/zhuji/p/7698057.html ...

  8. AspNetCore使用MySQL

    既然NetCore开源,那么也使用开源的MySQL的数据库呢?当然NetCore不止单单配MSSQL数据库而已.今天我来讲解NetCore怎么使用MySQL进行开发. 首先新建一个NetCore项目 ...

  9. 20191202IIS

    IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...

  10. 二分查找法(java版)

    二分查找法也称为折半查找法,在有序的序列中使用二分法可以提高程序的执行效率. 典型的二分查找法代码 public int binarySearch1(int[] arr,int target){ in ...