使用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

转载于:https://www.cnblogs.com/CSharpLover/p/5687263.html

WCF入门(五)---创建WCF服务相关推荐

  1. 【转】WCF Data Service 使用小结(二) —— 使用WCF Data Service 创建OData服务

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

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

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

  3. WCF Data Service 创建OData服务

    WCF Data Service 是需要 ORM 的支持的, 主流的 ORM 都已经支持 WCF Data Service 了, Entity Framework.Linq to SQL.ALinq  ...

  4. WCF入门-项目间调用服务

    练习WCF入库,添加了一个WCF项目,同时添加了一个控制台程序,在控制台程序启动时,调用WCF服务,报错. 控制台程序代码为: namespace WcfConsumer {class Program ...

  5. WCF学习之旅—WCF概述(四)

    一.WCF概述 1) 什么是WCF? Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终 ...

  6. 第五章 微服务网关Spring Cloud Gateway

    5.1 微服务网关简介 第三章我们介绍了通过Spring Cloud LoadBalancer实现了微服务之间的调⽤和负载均衡,以及使⽤Spring Cloud OpenFeign声明式调⽤,那我们的 ...

  7. WCF 入门之旅(4): 怎样用客户端调用WCF服务

    WCF 入门之旅(4): 怎样用客户端调用WCF服务 前面的demo已经能够跑起来,现在开始考虑用客户端来测试所写的应用了,首先用个普通的应用程序来调用所写的wcf服务吧.其实最后运行wcf服务后的页 ...

  8. WCF 第五章 并发和实例(服务行为)

    并发是一个多少个任务可以同时执行的度量,以任务(请求,工作,事务,以及类似的)的形式度量.执行时间是一个任务多长时间完成的度量,以时间(毫秒,秒,其他的时间单位等)度量.吞吐量是在一个指定时间里可以完 ...

  9. WCF 第五章 导出并发布元数据(服务行为)

    一个服务的ABCs-它的地址,绑定和契约-是以元数据的形式表示的以便于潜在的客户端知道到哪里,该如何与谁去通信.信息统称为服务元数据.服务行为元数据是大多数程序员遇到的第一个行为,因为当你创建WCF工 ...

最新文章

  1. 微软开源 Quantum Katas,领先的量子编程解决方案
  2. [c++][语言语法]函数模板和模板函数 及参数类型的运行时判断
  3. JAVA基础知识之网络编程——-基于AIO的异步Socket通信
  4. 经典排序算法(7)——堆排序算法详解
  5. 2018上半年软件设计师上午题参考答案
  6. UITableView基本用法
  7. Atitit..jdk java 各版本新特性 1.0 1.1 1.2 1.3 1.4 1.5(5.0) 1.6(6.0) 7.0 8.0 9.0 attilax 大总结...
  8. 根据XPATH去查看修改xml文件节点的内容
  9. chrome devTool
  10. sudo rpm安装时$USER是root
  11. vue 圆形 水波_canvas 水滴图、液体进度条、仿加速球、圆球水波图
  12. spss数据的预处理
  13. 国家代码查询(Country codes)
  14. 酒店系统主服务器是什么意思,酒店主服务器硬件配置
  15. 135、137、138、139、445等端口解释和关闭方法
  16. Flutter 解决软键盘弹出背景图片变形
  17. 刷微信点击量的php,PHP一键刷QQ微信支付宝步数代码
  18. Markdown (CSDN) MD编辑器(四)- 漂亮表格(表格背景色、跨行、跨列、多行)
  19. go区块链培训学习:密码学算法家族
  20. Failed to restart networking.service: Unit networking.service not found.

热门文章

  1. 李洋疯狂C语言之编程实现统计某年某月份的天数
  2. 用Kotlin撸一个图片压缩插件-实战篇(三)
  3. 第一个servlet小程序
  4. Nodejs从小工到专家系列(一)
  5. Spring web应用最大的败笔
  6. 第九周项目6-穷举法之年龄几何
  7. DevExpress控件使用经验总结
  8. 瑞星2009:3大拦截2大防御功能主动遏制木马病毒
  9. Samsung原版44B0X的Bootloader分析
  10. 企业应该如何选型ERP?