本篇將通過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)可以在方法中設置斷點單步調試。

