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.

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

  1. Step1 - How to: Define a Windows Communication Foundation Service Contract

    https://msdn.microsoft.com/en-us/library/ms731835.aspx This is the first of six tasks required to cr ...

  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. How to: Create a Windows Communication Foundation Client

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

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

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

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

    场景 WCF应用程序部署在IIS7中,使用net.tcp协议对外给几百台客户端提供服务,应用程序池不断崩溃重启. 分析过程 在事件查看器中看到的错误信息类似于 为应用程序池“XX”提供服务的进程在与 ...

  9. Workflow-Microsoft:Windows Workflow Foundation

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

随机推荐

  1. ios常用的一些类库

    在网上收集到的 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https:/ ...

  2. unity访问php

    长连接,弱联网.不好意思,这俩不是一个意思. 反过来说,短连接,强联网,是不是有点别扭呢. 你可以不会php,甚至你可以不知道php是干什么的. 百度php安装环境,自行搭建好环境,顺便测试一下.(下 ...

  3. angular2 select change 事件

    刚开始这是啥?(wrong!!!    change事件会在 选择option行为  之前执行prodDirId,是取不到选择后正确的id值的,取得是选择行为前prodDirId的值(有试过setTi ...

  4. java集合 collection-list-LinkedList 模拟一个堆栈或者队列数据结构。

    /* 使用LinkedList模拟一个堆栈或者队列数据结构. 堆栈:先进后出 如同一个杯子. 队列:先进先出 First in First out FIFO 如同一个水管. */ import jav ...

  5. 利用C语言获取设备的MAC address

    利用C语言获取设备的MAC address MAC address --> Medium Access Control layer address // // http://www.binary ...

  6. STL标准模板库 向量容器(vector)

    向量容器使用动态数组存储.管理对象.因为数组是一个随机访问数据结构,所以可以随机访问向量中的元素.在数组中间或是开始处插入一个元素是费时的,特别是在数组非常大的时候更是如此.然而在数组末端插入元素却很 ...

  7. 【nodemailer】 初试

    nodemailer 是什么? 简单的讲nodemailer就是用来发送邮件的.最近的一个项目需要向客户的注册邮箱发送验证连接,研究了一下. 刚开始我以为nodemailer还可以用来接收邮件,看了好 ...

  8. 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(四)

    这一节,是要把AS库和Android的jar包及相关配置文件打成一个ane包. 首先先建一个build目录,里面文件目录结构如下: 然后用打开压缩包的方式打开ServiceLib.swc, 把其中的l ...

  9. 2016031901 - ubuntu15.1安装驱动

    个人使用u盘安装的ubuntu15.1,安装后找不到无线,主要是驱动没有安装的问题. 解决方案如下: 01.wife无法找到 02.pool文件夹内都是驱动,我们需要的网络驱动也在内 03.网络驱动包 ...

  10. <四> jQuery 事件

    $(document).ready(function) 将函数绑定到文档的就绪事件(当文档完成加载时) $(selector).click(function) 触发或将函数绑定到被选元素的点击事件 $ ...