上一篇:NHibernate+WCF项目实战(二)使用NHibernate实现数据访问并进行单元测试

开发环境

    我的开发环境是VS2008 SP1+SQLServer 2005

    NHibernate版本是2.1.0.4000

  NUnit版本是2.5.2

  Microsoft Web Application Stress Tool 版本是1.1

本节概要


    上一篇完成了用户操作的三个方法,本篇将通过WCF以webservices的方式对外提供接口。同时使用NUnit对webservices中的方法进行单元测试。

另,上一篇对NHibernate的使用还有很多地方值的改善,同时也感谢园友李永京的指点。

开发契约 contract

Contract项目为类库项目,该项目下会包含WCF中的ServiceContract,这是一些被加上Attribute [ServiceContract]的接口。同时接口中的方法也需要加上Attribute [OperationContract]。

另,考虑到下一篇要对接口进行压力测试,所以接口中的方法也加上Attribute [WebGet],可以通过get方式访问方法。

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

using

    System.ServiceModel;

    System.ServiceModel.Web;//webGet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;//webGet

  7. namespace Lee.Contract
  8. {
  9.     [ServiceContract]
  10.     public interface IUserInfo
  11.     {
  12.         [OperationContract]
  13.         [WebGet]
  14.         bool AddUserInfo(string name, string description, string state);
  15.         [OperationContract]
  16.         [WebGet]
  17.         bool ExistUserInfo(string name);
  18.         [OperationContract]
  19.         [WebGet]
  20.         bool UpdateUserInfo(string name, string description, string state);
  21.     }
  22. }
复制代码
开发服务  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. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. using Lee.DAL;
  6. using Lee.Model;
  7. using Lee.Contract;

  8. namespace Lee.Services
  9. {
  10.     public class UserInfo:IUserInfo
  11.     {
  12.         /**//// <summary>
  13.         /// 添加用户
  14.         /// </summary>
  15.         /// <param name="name">用户名称</param>
  16.         /// <param name="description">用户描述</param>
  17.         /// <param name="state">状态</param>
  18.         /// <returns>True-操作成功|False-操作失败</returns>
  19.         public bool AddUserInfo(string name, string description, string state)
  20.         {
  21.             UserInfoDAL dal =new UserInfoDAL();
  22.             return dal.AddUserInfo(name,description,state);
  23.         }
  24.         /**//// <summary>
  25.         /// 检查用户是否存在
  26.         /// </summary>
  27.         /// <param name="name">用户名称</param>
  28.         /// <returns>True-用户存在|False-用户不存在</returns>
  29.         public bool ExistUserInfo(string name)
  30.         {
  31.             UserInfoDAL dal =new UserInfoDAL();
  32.             return dal.ExistUserInfo(name);
  33.         }
  34.         /**//// <summary>
  35.         /// 更新用户信息
  36.         /// </summary>
  37.         /// <param name="name">用户名称</param>
  38.         /// <param name="description">用户描述</param>
  39.         /// <param name="state">状态</param>
  40.         /// <returns>True-操作成功|False-操作失败</returns>
  41.         public bool UpdateUserInfo(string name, string description, string state)
  42.         {
  43.             UserInfoDAL dal = new UserInfoDAL();
  44.             return dal.UpdateUserInfo(name, description, state);
  45.         }
  46.     }
  47. }
复制代码
开发宿主 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. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   <connectionStrings>
  4.     <add name="SQLConnection" connectionString="Database=XX;User ID=sa;Password=saas;Server=XX;" providerName="System.Data.SqlClient"/>
  5.   </connectionStrings>
  6.   <system.serviceModel>
  7.     <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
  8.     <services>
  9.       <service behaviorConfiguration="Lee.Hosting.UserInfoBehavior" name="Lee.Services.UserInfo">
  10.         <endpoint address="" binding="basicHttpBinding" contract="Lee.Contract.IUserInfo">
  11.           <identity>
  12.             <dns value="localhost" />
  13.           </identity>
  14.         </endpoint>
  15.         <endpoint address="webhttp" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="Lee.Contract.IUserInfo">
  16.           <identity>
  17.             <dns value="localhost" />
  18.           </identity>
  19.         </endpoint>
  20.       </service>
  21.     </services>
  22.     <behaviors>
  23.       <endpointBehaviors>
  24.         <behavior name="webHttp">
  25.           <webHttp />
  26.         </behavior>
  27.       </endpointBehaviors>
  28.       <serviceBehaviors>
  29.         <behavior name="Lee.Hosting.UserInfoBehavior">
  30.           <serviceMetadata httpGetEnabled="true" />
  31.           <serviceDebug includeExceptionDetailInFaults="true" />
  32.         </behavior>
  33.       </serviceBehaviors>
  34.     </behaviors>
  35.   </system.serviceModel>
  36.   <system.web>
  37.     <compilation debug="true"/>
  38.   </system.web>
  39. </configuration>
复制代码
5)创建NHibernate配置文件hibernate.cfg.xml并设置为始终复制,添加对NHibernate和NHibernate.ByteCode.Castle的引用。
TOP