未来大家得以用 WCF 4
轻易营造一个REST
Service,怎样测试呢?
先是让我们来看类图:

XML 1

代码是这么的:

   1:      [ServiceContract]

   2:      public interface IDataService

   3:      {

   4:            [OperationContract]

   5:            [WebGet(UriTemplate = "/Get?id={id}")]

   6:            Employee Get(int id);

   7:   

   8:            [OperationContract]

   9:            [WebInvoke(UriTemplate = "/Update?id={id}", Method = "PUT")]

  10:            bool Update(int id);

  11:   

  12:            [OperationContract]

  13:            [WebInvoke(UriTemplate = "/Del?id={id}", Method = "DELETE")]

  14:            bool Delete(int id);

  15:   

  16:            [OperationContract]

  17:            [WebInvoke(UriTemplate = "Create", Method = "POST")]

  18:            bool Create(Employee employee);

  19:      }

Entity class:

   1:      [DataContract]

   2:      public partial class Employee 

   3:      {

   4:          public Employee()

   5:          {}

   6:   

   7:          [DataMember]

   8:          public int EmployeeID { get; set; }

   9:          [DataMember]

  10:          public string NationalIDNumber { get; set; }

  11:          [DataMember]

  12:          public int ContactID { get; set; }

  13:          [DataMember]

  14:          public string LoginID { get; set; }

  15:          [DataMember]

  16:          public Nullable<int> ManagerID { get; set; }

  17:          [DataMember]

  18:          public string Title { get; set; }

  19:          [DataMember]

  20:          public System.DateTime BirthDate { get; set; }

  21:          [DataMember]

  22:          public string MaritalStatus { get; set; }

  23:          [DataMember]

  24:          public string Gender { get; set; }

  25:          [DataMember]

  26:          public System.DateTime HireDate { get; set; }

  27:          [DataMember]

  28:          public bool SalariedFlag { get; set; }

  29:          [DataMember]

  30:          public short VacationHours { get; set; }

  31:          [DataMember]

  32:          public short SickLeaveHours { get; set; }

  33:          [DataMember]

  34:          public bool CurrentFlag { get; set; }

  35:          [DataMember]

  36:          public System.Guid rowguid { get; set; }

  37:          [DataMember]

  38:          public System.DateTime ModifiedDate { get; set; }

  39:      }

弹无虚发的service,为了演示那里只是简单已毕。

   1:      /// <summary>

   2:      /// CRUD Service  http://www.cnblogs.com/wintersun

   3:      /// </summary>

   4:      public class CRUDService : IDataService

   5:      {

   6:          public Employee Get(int id)

   7:          {

   8:              return new Employee() { EmployeeID = 11, Title = "Engineer" };

   9:          }

  10:   

  11:          public bool Update(int id)

  12:          {

  13:              //TODO

  14:              return true;

  15:          }

  16:   

  17:          public bool Delete(int id)

  18:          {

  19:              //TODO

  20:              return true;

  21:          }

  22:   

  23:          public bool Create(Employee employee)

  24:          {

  25:              //TODO

  26:              return true;

  27:          }

  28:      }

Config文件是如此的:

   1:    <system.serviceModel>

   2:      <services>

   3:        <service name="webdemo.CRUDService" behaviorConfiguration="ServiceBehaviour">

   4:          <endpoint address=""  binding="webHttpBinding" contract="webdemo.IDataService" behaviorConfiguration="web">

   5:          </endpoint>

   6:        </service>

   7:      </services>

   8:      <behaviors>

   9:        <endpointBehaviors>

  10:          <behavior name="web">

  11:            <webHttp />

  12:          </behavior>

  13:        </endpointBehaviors>

  14:        <serviceBehaviors>

  15:          <behavior name="ServiceBehaviour">

  16:            <serviceMetadata httpGetEnabled="true" />

  17:            <serviceDebug includeExceptionDetailInFaults="false" />

  18:          </behavior>

  19:          <behavior>

  20:            <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

  21:            <serviceMetadata httpGetEnabled="true"/>

  22:            <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->

  23:            <serviceDebug includeExceptionDetailInFaults="false"/>

  24:          </behavior>

  25:        </serviceBehaviors>

  26:      </behaviors>

  27:      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

  28:    </system.serviceModel>

留神,我们运用是webHttpBinding,在DEBUG时方可把includeExceptionDetailInFaults设置为true.
我们通用会利用Svcutil.exe变化proxy类,但对webHttpBinding是不会生成Output.config的。

当大家编译,Host那一个Service,你从浏览器中开辟类似那样的URL:

http://localhost:50422/CRUDService.svc

调用Get的时序图,其余方法类似了。

XML 2

对此Http Get 操作我们可以直接在浏览器测试,当大家走访

http://localhost:50422/CRUDService.svc/Get?Id=1

XML 3

我们用Fiddler来测试,它有一个效应是Request
Builder,上边来测试Get方法, Request RAW:

GET http://localhost:50422/CRUDService.svc/Get?id=1 HTTP/1.1
User-Agent: Fiddler
Host: localhost:50422

下一场Response RAW是那样的:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 28 Jun 2011 06:18:05 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 651
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Connection: Close

<Employee
xmlns=”http://schemas.datacontract.org/2004/07/webdemo%22
xmlns:i=”http://www.w3.org/2001/XMLSchema-instance%22><BirthDate>0001-01-01T00:00:00</BirthDate><ContactID>0</ContactID><CurrentFlag>false</CurrentFlag><EmployeeID>11</EmployeeID><Gender
i:nil=”true”/><HireDate>0001-01-01T00:00:00</HireDate><LoginID
i:nil=”true”/><ManagerID i:nil=”true”/><MaritalStatus
i:nil=”true”/><ModifiedDate>0001-01-01T00:00:00</ModifiedDate><NationalIDNumber
i:nil=”true”/><SalariedFlag>false</SalariedFlag><SickLeaveHours>0</SickLeaveHours><Title>Engineer</Title><VacationHours>0</VacationHours><rowguid>00000000-0000-0000-0000-000000000000</rowguid></Employee>

Request PUT :

PUT http://localhost:50422/CRUDService.svc/Update?id=1 HTTP/1.1
User-Agent: Fiddler
Host: localhost:50422

Response RAW:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 28 Jun 2011 06:20:15 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 83
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Connection: Close

<boolean
xmlns=”http://schemas.microsoft.com/2003/10/Serialization/%22>true</boolean>

HTTP-PUT操作对大家的Update方法。接着是HTTP-DELETE, Request header RAW :

DELETE http://localhost:50422/CRUDService.svc/Del?id=1 HTTP/1.1
User-Agent: Fiddler
Host: localhost:50422

Response RAW:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 28 Jun 2011 06:21:01 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 83
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Connection: Close

<boolean
xmlns=”http://schemas.microsoft.com/2003/10/Serialization/%22>true</boolean>

最终测试的是HTTP-POST方法,这么些大家比较熟谙. Request RAW header:

POST http://localhost:50422/CRUDService.svc/Create HTTP/1.1
User-Agent: Fiddler
Content-Type: application/xml; charset=utf-8
Host: localhost:50422
Content-Length: 652

与前边差别大家还须要POST一个BODY,在Server端WCF会帮大家把XML反系列化成Object. 

<Employee
xmlns=”http://schemas.datacontract.org/2004/07/webdemo%22
xmlns:i=”http://www.w3.org/2001/XMLSchema-instance%22><BirthDate>0001-01-01T00:00:00</BirthDate><ContactID>0</ContactID><CurrentFlag>false</CurrentFlag><EmployeeID>11</EmployeeID><Gender
i:nil=”true”/><HireDate>0001-01-01T00:00:00</HireDate><LoginID
i:nil=”true”/><ManagerID i:nil=”true”/><MaritalStatus
i:nil=”true”/><ModifiedDate>0001-01-01T00:00:00</ModifiedDate><NationalIDNumber
i:nil=”true”/><SalariedFlag>false</SalariedFlag><SickLeaveHours>0</SickLeaveHours><Title>asdfasd</Title><VacationHours>0</VacationHours><rowguid>00000000-0000-0000-0000-000000000000</rowguid></Employee>

那边是按照XML的格式数据。看截图:

XML 4

POST方法Response RAW:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 28 Jun 2011 06:22:29 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 83
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Connection: Close

<boolean
xmlns=”http://schemas.microsoft.com/2003/10/Serialization/%22>true</boolean>

面前一回操作如下图,您或然注意到这几个方法都回去是bool值,对应上边RAW

XML 5

大家看出再次回到都是200就OK了。强烈提出你使用VS来DEBUG整个流程。那么些REST
Service提供了CRUD刚好对应了Http-GET,Http-POST,Http-DELETE,Http-PUT的操作。实际行使进度,大概还须求注意安全方面的要素。

下五次将为你介绍另一种方法测试基于Web WCF 瑟维斯。

你恐怕感兴趣的篇章:

C#兑现一个几乎的REST
service

希望那篇POST对您支付有接济。

作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转发,但未经作者同意必须保留此段注解,且在小说页面显然地点给出原文连接,否则保留追究法律权利的职务。
该文章也同时发布在自我的独门博客中-Petter Liu
Blog

相关文章

网站地图xml地图