本篇将通过WCF以webservices的方式对外提供接口。同时使用NUnit对webservices中的方法进行单元测试。

开发契约 contract

Contract项目为类库项目,该项目下会包含WCF中的ServiceContract,这是一些被加上Attribute [ServiceContract]的接口。同时接口中的方法也需要加上Attribute [OperationContract]。
另,考虑到下一篇要对接口进行压力测试,所以接口中的方法也加上Attribute [WebGet],可以通过get方式访问方法。

下面就开始定义UserInfo的Contract—IuserInfo接口。

using

System.ServiceModel;

System.ServiceModel.Web;//webGet

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. using System.ServiceModel;
  8. using System.ServiceModel.Web;//webGet
  9.  
  10. namespace Lee.Contract
  11. {
  12.     [ServiceContract]
  13.     public interface IUserInfo
  14.     {
  15.         [OperationContract]
  16.         [WebGet]
  17.         bool AddUserInfo(string name, string description, string state);
  18.         [OperationContract]
  19.         [WebGet]
  20.         bool ExistUserInfo(string name);
  21.         [OperationContract]
  22.         [WebGet]
  23.         bool UpdateUserInfo(string name, string description, string state);
  24.     }
  25. }

开发服务  Services

Services项目也是类库项目,该项目主要是对Contract的具体实现,同时会调用DAL提供的数据访问层方法。

using

添加对Lee.Model项目的引用。

添加对Lee.DAL项目的引用。

添加对Lee. Contract项目的引用。

我们实现的UserInfo的三个方法中都是返回了Bool值,如果方法返回对象,这时就需要添加对Lee.Model项目的引用。
另,如果要在WCF中传递对象,需要为实体类添加Attribute [DataContract]和[Serializable]。属性需要添加Attribute [DataMember]。

下面是Lee.Services中的UserInfo 服务类

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. using Lee.DAL;
  8. using Lee.Model;
  9. using Lee.Contract;
  10.  
  11. namespace Lee.Services
  12. {
  13.     public class UserInfo:IUserInfo
  14.     {
  15.         /**//// <summary>
  16.         /// 添加用户
  17.         /// </summary>
  18.         /// <param name="name">用户名称</param>
  19.         /// <param name="description">用户描述</param>
  20.         /// <param name="state">状态</param>
  21.         /// <returns>True-操作成功|False-操作失败</returns>
  22.         public bool AddUserInfo(string name, string description, string state)
  23.         {
  24.             UserInfoDAL dal =new UserInfoDAL();
  25.             return dal.AddUserInfo(name,description,state);
  26.         }
  27.         /**//// <summary>
  28.         /// 检查用户是否存在
  29.         /// </summary>
  30.         /// <param name="name">用户名称</param>
  31.         /// <returns>True-用户存在|False-用户不存在</returns>
  32.         public bool ExistUserInfo(string name)
  33.         {
  34.             UserInfoDAL dal =new UserInfoDAL();
  35.             return dal.ExistUserInfo(name);
  36.         }
  37.         /**//// <summary>
  38.         /// 更新用户信息
  39.         /// </summary>
  40.         /// <param name="name">用户名称</param>
  41.         /// <param name="description">用户描述</param>
  42.         /// <param name="state">状态</param>
  43.         /// <returns>True-操作成功|False-操作失败</returns>
  44.         public bool UpdateUserInfo(string name, string description, string state)
  45.         {
  46.             UserInfoDAL dal = new UserInfoDAL();
  47.             return dal.UpdateUserInfo(name, description, state);
  48.         }
  49.     }
  50. }

开发宿主 Hosting

Hosting项目为WCF服务应用程序,该项目会自动添加对System.Runtime.Serialization和System.ServiceModel的引用。

using

添加对Lee. Contract项目的引用。

添加对Lee. Services项目的引用。

详细步骤

1)添加 UserInfo.svc;

2)删除文件 UserInfo.svc.cs;

3)双击打开 UserInfo.svc

<%@ ServiceHost Language="C#" Debug="true" Service="Lee.Hosting.UserInfo" CodeBehind="UserInfo.svc.cs" %>

修改为:

<%@ ServiceHost Language="C#" Debug="true" Service="Lee.Services.UserInfo" CodeBehind="Lee.Services.UserInfo.cs" %>

4)修改Web.config;

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <configuration>
  4.   <connectionStrings>
  5.     <add name="SQLConnection" connectionString="Database=XX;User ID=sa;Password=saas;Server=XX;" providerName="System.Data.SqlClient"/>
  6.   </connectionStrings>
  7.   <system.serviceModel>
  8.     <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
  9.     <services>
  10.       <service behaviorConfiguration="Lee.Hosting.UserInfoBehavior" name="Lee.Services.UserInfo">
  11.         <endpoint address="" binding="basicHttpBinding" contract="Lee.Contract.IUserInfo">
  12.           <identity>
  13.             <dns value="localhost" />
  14.           </identity>
  15.         </endpoint>
  16.         <endpoint address="webhttp" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="Lee.Contract.IUserInfo">
  17.           <identity>
  18.             <dns value="localhost" />
  19.           </identity>
  20.         </endpoint>
  21.       </service>
  22.     </services>
  23.     <behaviors>
  24.       <endpointBehaviors>
  25.         <behavior name="webHttp">
  26.           <webHttp />
  27.         </behavior>
  28.       </endpointBehaviors>
  29.       <serviceBehaviors>
  30.         <behavior name="Lee.Hosting.UserInfoBehavior">
  31.           <serviceMetadata httpGetEnabled="true" />
  32.           <serviceDebug includeExceptionDetailInFaults="true" />
  33.         </behavior>
  34.       </serviceBehaviors>
  35.     </behaviors>
  36.   </system.serviceModel>
  37.   <system.web>
  38.     <compilation debug="true"/>
  39.   </system.web>
  40. </configuration>

5)创建NHibernate配置文件hibernate.cfg.xml并设置为始终复制,添加对NHibernate和NHibernate.ByteCode.Castle的引用。

6)效果查看

浏览UserInfo.svc

对应的WSDL

查看Schema格式

到现在为止,我们已经用WCF成功的对外发布了接口。下面我们对Webservices进行单元测试!

单元测试

单元测试的相关设置在上一篇已经讲过了,这里不再介绍。

测试步骤

1)using

添加对Lee. Contract项目的引用。

2)添加服务引用,直接点“发现“,可以找到该解决方案下的服务。

  成功添加后,会自动在App.Config中创建client端EndPoint。

3)创建服务测试类TestUserInfoSVC.cs

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. using Lee.Model;
  8. using Lee.DAL;
  9. using NUnit.Framework;
  10.  
  11. namespace Lee.Test
  12. {
  13.     [TestFixture]
  14.     public class TestUserInfoSVC
  15.     {
  16.         [Test]
  17.         public void AddUserInfo()
  18.         {
  19.             UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
  20.             bool result = client.AddUserInfo("testname6", "testdesc", "teststate");
  21.             Assert.AreEqual(true, result);
  22.         }
  23.         [Test]
  24.         public void ExistUserInfo()
  25.         {
  26.             UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
  27.             bool result = client.ExistUserInfo("testname");
  28.             Assert.AreEqual(true, result);
  29.         }
  30.         [Test]
  31.         public void UpdateUserInfo()
  32.         {
  33.             UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
  34.             bool result = client.UpdateUserInfo("testname5", "hello,testname!", "activation");
  35.             Assert.AreEqual(true, result);
  36.         }
  37.     }
  38. }

4)可以在方法中设置断点单步调试。

使用WCF对外提供接口的更多相关文章

  1. springboot+CXF开发webservice对外提供接口(转)

    文章来源:http://www.leftso.com/blog/144.html 1.项目要对外提供接口,用webservcie的方式实现 2.添加的jar包 maven: <dependenc ...

  2. 开发FTP服务接口,对外提供接口服务

    注意:本文只适合小文本文件的上传下载,因为post请求是有大小限制的.默认大小是2m,虽然具体数值可以调节,但不适合做大文件的传输 最近公司有这么个需求:以后所有的项目开发中需要使用ftp服务器的地方 ...

  3. Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问

    Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问 1.使用场景: 需求1.家中服务器 ubuntu 主机,跑接口服务,需要对外暴漏, 需求2.同时需要在外网ssh远程 ​ 关键词: frp内网 ...

  4. Java服务器对外提供接口以及Android端向服务器请求数据

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5056780.html 讲解下java服务器是如何对移动终端提供接口的,以什么数据格式提供出去,移动端又是怎么 ...

  5. WPF内嵌WCF服务对外提供接口

    要测试本帖子代码请记得管理员权限运行vs. 我写这个帖子的初衷是在我做surface小车的时候有类似的需求,感觉这个功能还挺有意思的,所以就分享给大家,网上有很多关于wcf的文章 我就不一一列举了.公 ...

  6. C++中模块(Dll)对外暴露接口的方式

    总结下C++中模块(Dll)对外暴露接口的方式: (1)导出API函数的方式这种方式是Windows中调用DLL接口的最基本方式,GDI32.dll, User32.dll都是用这种方式对外暴露系统A ...

  7. WCF中修改接口或步骤名称而不影响客户端程序

    WCF中修改接口或方法名称而不影响客户端程序 本篇接着"从Web Service和Remoting Service引出WCF服务"中有关WCF的部分. 运行宿主应用程序. 运行We ...

  8. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  9. grpc-gateway:grpc对外提供http服务的解决方案

    我所在公司的项目是采用基于Restful的微服务架构,随着微服务之间的沟通越来越频繁,就希望可以做成用rpc来做内部的通讯,对外依然用Restful.于是就想到了google的grpc. 使用grpc ...

随机推荐

  1. 字符集与Mysql字符集处理(一)

      一.字符集总结 其实大多数的知识在这篇文章里已经讲得非常清楚了.这里只是讲一下自己的感悟. 1. UTF-8虽然是以UTF(unicode transfermation format)开头的,但是 ...

  2. MyBatis 元素类型为 "configuration" 的内容必须匹配 ".....

    修改MyBatis配置文件时,添加typeAliases节点,报了一个BuilderException: org.apache.ibatis.exceptions.PersistenceExcepti ...

  3. 编写高质量JS代码的68个有效方法(十)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  4. Python单元测试框架之pytest---如何执行测试用例

    介绍   pytest是一个成熟的全功能的Python测试工具,可以帮助你写出更好的程序. 适合从简单的单元到复杂的功能测试 l 模块化parametrizeable装置(在2.3,持续改进) l 参 ...

  5. iOS-定时器

    一.定时器的作用 在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 二.定时器的种类 大概有三种方法:NSTimer.CA ...

  6. Android 学习笔记 Service

    PS:前几篇的内容光是上代码了,也没有细细的讲解..感觉这样写很不好..因此还是多一些讲解吧... 学习内容: 1.了解Service... 2.Service的启动与停止.. 3.绑定与取消绑定Se ...

  7. CentOS6.5菜鸟之旅:关于搜索的shell命令

    一.locate命令 用于模糊搜索文件(目录)的绝对路径. 示例1: // 凡是绝对路径当中含jdk字符串的文件(目录)均被搜索出来 fsjohnhuang@fsjohnhuang~# locate ...

  8. Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划)

    Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到 ...

  9. ASP.NET MVC5---通过QueryString传值

    1.首先我们来看看普通的传值是啥样的.如图所示,我们普通的传值是这样的 public ActionResult Edit(int?id) { if (id == null) { return new ...

  10. 数论 - 算数基本定理的运用 --- nefu 118 : n!后面有多少个0

     题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php Mean: 略. analyse: 刚开始想了半天都没想出来,数据这么大,难道是有什么 ...