本篇将通过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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.ServiceModel;
using System.ServiceModel.Web;//webGet namespace Lee.Contract
{
    [ServiceContract]
    public interface IUserInfo
    {
        [OperationContract]
        [WebGet]
        bool AddUserInfo(string name, string description, string state);
        [OperationContract]
        [WebGet]
        bool ExistUserInfo(string name);
        [OperationContract]
        [WebGet]
        bool UpdateUserInfo(string name, string description, string state);
    }
}

开发服务  Services

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

using

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

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

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

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

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Lee.DAL;
using Lee.Model;
using Lee.Contract; namespace Lee.Services
{
    public class UserInfo:IUserInfo
    {
        /**//// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="name">用户名称</param>
        /// <param name="description">用户描述</param>
        /// <param name="state">状态</param>
        /// <returns>True-操作成功|False-操作失败</returns>
        public bool AddUserInfo(string name, string description, string state)
        {
            UserInfoDAL dal =new UserInfoDAL();
            return dal.AddUserInfo(name,description,state);
        }
        /**//// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="name">用户名称</param>
        /// <returns>True-用户存在|False-用户不存在</returns>
        public bool ExistUserInfo(string name)
        {
            UserInfoDAL dal =new UserInfoDAL();
            return dal.ExistUserInfo(name);
        }
        /**//// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="name">用户名称</param>
        /// <param name="description">用户描述</param>
        /// <param name="state">状态</param>
        /// <returns>True-操作成功|False-操作失败</returns>
        public bool UpdateUserInfo(string name, string description, string state)
        {
            UserInfoDAL dal = new UserInfoDAL();
            return dal.UpdateUserInfo(name, description, state);
        }
    }
}

开发宿主 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;


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="SQLConnection" connectionString="Database=XX;User ID=sa;Password=saas;Server=XX;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
    <services>
      <service behaviorConfiguration="Lee.Hosting.UserInfoBehavior" name="Lee.Services.UserInfo">
        <endpoint address="" binding="basicHttpBinding" contract="Lee.Contract.IUserInfo">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="webhttp" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="Lee.Contract.IUserInfo">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Lee.Hosting.UserInfoBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.web>
    <compilation debug="true"/>
  </system.web>
</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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Lee.Model;
using Lee.DAL;
using NUnit.Framework; namespace Lee.Test
{
    [TestFixture]
    public class TestUserInfoSVC
    {
        [Test]
        public void AddUserInfo()
        {
            UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
            bool result = client.AddUserInfo("testname6", "testdesc", "teststate");
            Assert.AreEqual(true, result);
        }
        [Test]
        public void ExistUserInfo()
        {
            UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
            bool result = client.ExistUserInfo("testname");
            Assert.AreEqual(true, result);
        }
        [Test]
        public void UpdateUserInfo()
        {
            UserInfoSVC.UserInfoClient client = new Lee.Test.UserInfoSVC.UserInfoClient();
            bool result = client.UpdateUserInfo("testname5", "hello,testname!", "activation");
            Assert.AreEqual(true, result);
        }
    }
}

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. 魔兽争霸3 replay 格式

    ******************************************************************************* * WarCraft III Repla ...

  2. 未能找到类型或命名空间名称“Coco”(是否缺少 using 指令或程序集引用)

    未能找到类型或命名空间名称"Coco"(是否缺少 using 指令或程序集引用),如果你确实引用了,那说明你引用的和你的项目环境版本不一样,.NET framework的问题,修改 ...

  3. JAVA和C# 3DES加密解密

    最近 一个项目.net 要调用JAVA的WEB SERVICE,数据采用3DES加密,涉及到两种语言3DES一致性的问题, 下面分享一下, 这里的KEY采用Base64编码,便用分发,因为Java的B ...

  4. 让文档和Demo生成更加简单和强大 - SmartDoc 0.1.1 说明

    新特性 smartDoc 0.1.1版正式发布,其中加入了更多方便生成文档的功能,主要特性如下: * 加入@demo配置项,看可以动态抓取html和js的内容作为@example,同时支持扩展@dem ...

  5. 12306外包给阿里巴巴/IBM到底是否可行?

    春运开始以后 12306 免不了要罢工几次,毕竟人民群众买票回家的热情实在是高涨,12306 很难承受如此大的压力.每次 12306 网站罢工以后都会有人忍不住对其进行吐槽,而还有人认为如果把 123 ...

  6. java中图片文件的判断

    javax.imageio 类 ImageIO BufferedImage bi = ImageIO.read(resFile);//resFile --- InputStream if(bi == ...

  7. DDD:聊天笔记

    聚合跟和实体 聚合根是实体. 实体有生命周期,使用标识进行跟踪. 聚合根是全局标识,由仓储或其它服务负责其生命周期. 实体是局部标识,由聚合根负责其生命周期. 为什么能应对复杂度? 纵向.横向.时间维 ...

  8. 使用 IntelliJ IDEA 2016和Maven创建Java Web项目的详细步骤及相关问题解决办法

    Maven简介 相对于传统的项目,Maven 下管理和构建的项目真的非常好用和简单,所以这里也强调下,尽量使用此类工具进行项目构建, 它可以管理项目的整个生命周期. 可以通过其命令做所有相关的工作,其 ...

  9. 受限玻尔兹曼机(RBM)学习笔记(三)能量函数和概率分布

      去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算 ...

  10. css中zoom和transform:scale的区别

    css中zoom和transform:scale的区别 关于zoom: 以前只是看到别人的代码中用过zoom,自己从未使用过,今天在探究ie7兼容inline-block时,发现里面提到了zoom.下 ...