使用Microsoft Visual Studio2012创建WCF服务,理解如下所有必要的编码,更好地创建WCF服务的概念,这里做一个简单的任务。

  • 启动Visual Studio 2012。

  • 单击新建项目,然后在Visual C#标签,选择WCF选项。

WCF服务创建,执行如加法,减法,乘法和除法基本的算术运算。主要的代码是在两个不同的文件 - 一个接口和一个类。

一个WCF中包含一个或多个接口和实现类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WcfServiceLibrary1
{
//NOTE: You can use the "Rename" command on the "Refactor" menu to change the
//interface name "IService1" in both code and config file together.
[ServiceContract]
Public interface IService1
{
[OperationContract]
int sum(int num1, int num2); [OperationContract]
int Subtract(int num1, int num2); [OperationContract]
int Multiply(int num1, int num2); [OperationContract]
int Divide(int num1, int num2);
} //Use a data contract as illustrated in the sample below to add composite types
//to service operations.
[DataContract]
Public class CompositeType
{
Bool boolValue = true;
String stringValue = "Hello "; [DataMember]
Public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
} [DataMember] Public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}

而其后面是类的代码,

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingSystem.Text; namespace WcfServiceLibrary1
{
//NOTE: You can use the "Rename" command on the "Refactor" menu to change the
//class name "Service1" in both code and config file together. publicclassService1 :IService1
{
/// This Function Return summation of two integer numbers publicint sum(int num1, int num2)
{
return num1 + num2;
}
///This function returns subtraction of two numbers.
///If num1 is smaller than number two then this function returns 0. publicint Subtract(int num1, int num2)
{
if (num1 > num2)
{
return num1 - num2;
}
else
{
return 0;
}
}
///This function returns multiplication of two integer numbers. publicint Multiply(int num1, int num2)
{
return num1 * num2;
}
///This function returns integer value of two integer number.
///If num2 is 0 then this function returns 1. publicintDivide(int num1, int num2)
{
if (num2 != 0)
{
return (num1 / num2);
}
else
{
return 1;
}
}
}
}

要运行此服务,请在Visual Studio中点击开始按钮。

当我们运行这个服务,下面的屏幕会出现。

上点击sum方法,在下面的页面将被打开。在这里,可以输入任何两个整数,然后单击Invoke按钮。该服务将返回这两个数字的总和。

像求和,我们可以执行哪个都列在菜单中的所有算术运算。这里是捕捉他们。

当点击下页将出现在Sutbtarct方法。输入整数,点击调用按钮,得到的输出如下所示。

下页将出现在Multiply方法单击时。输入整数,点击调用按钮,得到的输出如下所示。

下面的页面上会出现当点击Divide方法时。输入整数,点击调用按钮,得到的输出如下所示。

一旦服务被调用,可以在它们之间,直接从这里切换。

原文地址:http://www.yiibai.com/wcf/wcf_creating_services.html

WCF入门(五)---创建WCF服务的更多相关文章

  1. 使用WCF Data Service 创建OData服务

    使用WCF Data Service 创建OData服务 在 上一章 中,介绍了如何通过 OData 协议来访问 OData 服务提供的资源.下面来介绍如何创建一个 OData 服务.在这篇文章中,主 ...

  2. DevExpress XtraReports 入门五 创建交叉表报表

    原文:DevExpress XtraReports 入门五 创建交叉表报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更多的人不会像我这样浪 ...

  3. WCF Data Service 使用小结(二) —— 使用WCF Data Service 创建OData服务

    在 上一章 中,介绍了如何通过 OData 协议来访问 OData 服务提供的资源.下面来介绍如何创建一个 OData 服务.在这篇文章中,主要说明在.NET的环境下,如何使用 WCF Data Se ...

  4. Wcf for wp8 创建wcf服务 连接wp8模拟器并显示来自wcf服务的接口信息 (一)

    下载: vs2012 pro for wp8 iis express http://download.microsoft.com/download/B/2/8/B2801FEE-9A60-4AFA-8 ...

  5. WCF入门, 到创建一个简单的WCF应用程序

    什么是WCF?  WCF, 英文全称(windows Communication Foundation) , 即为windows通讯平台. windows想到这里大家都知道了 , WCF也正是由微软公 ...

  6. WCF入门(十)——服务对象模型

    当发生一次WCF请求-响应操作时,会经过如下几个步骤 WCF Client想WCF Server发送一个服务请求 WCF Server创建WCF服务对象 WCF Server调用WCF服务对象接口,将 ...

  7. WCF入门教程三[WCF的宿主]

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  8. 无废话WCF入门教程三[WCF的宿主]

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  9. 【转】WCF入门教程三[WCF的宿主]

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  10. WCF入门教程3——WCF通信模式

    本章内容 请求/响应模式 单工模式 双工模式 WCF异步调用 请求与响应模式 请求/响应     请求/响应通信是指客户端向服务端发送消息后,服务端会向客户端发送响应.这也意味着在接收到服务的响应以前 ...

随机推荐

  1. VS2010配色方案

    http://studiostyl.es/ 导入步骤:  工具------------导入和导出设置------------导入选定的环境设置------------否,仅导入新设置--------- ...

  2. cadence 16.6 Pspice 仿真步骤

    从ADI官网下载后缀为 cir 的文件,AD8210 为例 进行仿真 1 打开 Cadence -> Release 16.6 -> PSpice Accessories -> Mo ...

  3. Basic Operation about Linux

    1. 永久开启/关闭防火墙 在linux中防火墙是一个名叫iptables的工具 开启: chkconfig iptables on 关闭: chkconfig iptables off 即时生效,重 ...

  4. centos 格式化硬盘并挂载,添加重启后生效

    [root@cloud /]# passwd 更改用户 root 的密码 . 新的 密码: 重新输入新的 密码: passwd: 所有的身份验证令牌已经成功更新. [root@cloud /]# fd ...

  5. 20145120 《Java程序设计》第3周学习总结

    20145120 <Java程序设计>第3周学习总结 教材学习内容总结 基本类型与类类型的概念 在java里使用数组和字符串 封装的概念 在java定义函数 重载的概念 static的概念 ...

  6. Javascript 面向对象编程

    Javascript是一个类C的语言,他的面向对象的东西相对于C++/Java比较奇怪,但是其的确相当的强大,在 Todd 同学的“对象的消息模型”一文中我们已经可以看到一些端倪了.这两天有个前同事总 ...

  7. ffmpeg 从视频流中抓取图片

    从视频中不断抓取图片的基本流程:打开视频流地址->获取视频流packt->解码成图片帧->输出图片 一.初始化Ffmpeg void ffmpegInit(){ av_registe ...

  8. 生成最小树prim算法

    最小生成树prim算法实现   ‘      ’最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int G[6][6];       G[1] ...

  9. [book]awesome-machine-learning books

    https://github.com/josephmisiti/awesome-machine-learning/blob/master/books.md Machine-Learning / Dat ...

  10. org.apache.commons.dbutils.QueryRunner 执行sqlserver的存储过程

    执行不带输出参数的存储过程与 执行普通update  sql没有什么区别,直接调用即可: 示例代码: public Boolean startResidentialInfoStatistics(Str ...