Windows Communication Foundation (WCF) 是一个用来在Windows上开发和实现服务的SDK。WCF为你的服务提供了Runtime环境,使得你能够将CLR类展开成服务,并且可以把其他服务当做CLR类来试用。在这篇文章中,我将解释如何使用WCF4.0来实现“REST”的服务API。
根据Roy Fielding的理论:“Representational State Transfer (REST),尝试将结构化的样式标准化,并设计出使得Web名副其实的约束条件。REST强调概念和层次的划分、无状态和存储这类的东西,这类东西以他们提供的优势,而在许多分布式的体系结构中十分常见。这些优势包括交互性、独立的改进、拦截、改进的可扩展性、效率以及总体上的性能。”
实际上区别仅仅是客户端访问我们的服务的方式。通常的,一个WCF服务会使用SOAP,但是如果你构建了一个REST服务,客户端会使用一个不同的结构样式来访问你的服务(调用,如JSON一般的序列化,等等)。
REST使用一些常见的HTTP方法来插入、删除、更新、返回信息,这些方法如下:
几天之前,我正在编写一个服务,它能够支持多种语言、平台或系统的访问。它可以被iPhone、Android、Windows Phone、.Net Web应用、Java或者PHP所使用。使用Web服务,使用统一的系统来将它展现给每个人,这对我来说有点复杂。之后,我决定使用REST,它更容易支持云。这是一个很好的例子,它表明了简单的“REST”的服务 :)。下面是一些重点,他们能够帮助你理解为什么使用“REST”的服务。
创建名为WCFClasses的类库。在类库中创建类。
1. IGasPriceService.cs
WCF操作数据契约的代码如下:
[ServiceContract] public interface IGasPriceService { [OperationContract] [WebGet (ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetGasPrice/For/ZipCode/{zipCode}" )] GasPriceData GetPriceByZipCode(string zipCode); [OperationContract] [WebGet (RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetGasPrice/For/City/{city}" )] GasPriceData GetPriceByCity(string city); }
WebGet属性让我们能够定制在处理REST服务的过程中,如何处理HTTP的GET方法。如例子所示,我们在URL中呈现GET方法,并且我们期待返回一些POX(简单的旧式XML)结果。
裸格式:
<GasPriceData> <GasStation>HP</GasStation> <Price>459</Price> </GasPriceData>有包装的格式:
<GetPriceDataResponse> <GetPriceDataResult> <a:GasStation>HP</a:GasStation> <a:Price>459</a:Price> </GetPriceDataResult> </GetPriceDataResponse>
UriTemplate描述了我们将如何处理 Uri映射。这个例子中,我们声明了基地址(BaseAddress)之后的URL看起来是下面这样的:
/GetGasPrice/For/ZipCode/{zipCode}
这就是服务合同,为我们REST服务实现了两个方法。一个通过城市名字得到天然气价格,另一个通过邮政编码获得。
Class 2: GasPriceService.cs
这个类是用来将数据返回给服务端。在现实场景中,它可能会连接到一些可以返回数据的数据存储区。
using System.ServiceModel.Activation; namespace WCFClasses { [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class GasPriceService : IGasPriceService { #region Members public GasPriceData GetPriceByZipCode(string zipCode) { switch (zipCode) { case "00000": return new GasPriceData { GasStation = "None", Price = 0.00m }; case "12345": return new GasPriceData {GasStation = "HP", Price = 420m}; case "11111": return new GasPriceData {GasStation = "Indian Oil", Price = 531m}; default: return new GasPriceData {GasStation = "DefaultGasPrice", Price = 400m}; } } public GasPriceData GetPriceByCity(string city) { switch (city.ToLower()) { case "hyderabad": return new GasPriceData { GasStation = "HP", Price = 459m }; case "delhi": return new GasPriceData { GasStation = "Indian Oil", Price = 449m }; case "chenni": return new GasPriceData { GasStation = "BG", Price = 499m }; default: return new GasPriceData { GasStation = "DefaultGasPrice", Price = 409m }; } } #endregion } }
举个用途例子,本端携带有天然气价格数据。当然作为一个实际的服务,这应该被实现为一个更健壮的对象模型。
using System.Runtime.Serialization; namespace WCFClasses { [DataContract] public class GasPriceData { [DataMember] public string GasStation { get; set; } [DataMember] public decimal Price { get; set; } } }
托管代码如下:
代码:Application_Start中:
protected void Application_Start(object sender, EventArgs e) { System.ServiceModel.Activation.WebServiceHostFactory WSHF = new System.ServiceModel.Activation.WebServiceHostFactory(); System.ServiceModel.Activation.ServiceRoute ss = new System.ServiceModel.Activation.ServiceRoute( "wcfservice", WSHF, typeof(WCFClasses.GasPriceService)); System.Web.Routing.RouteTable.Routes.Add(ss); }
<configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> </configuration>
现在如果我用http://localhost:1486/wcfservice/GetGasPrice/For/ZipCode/11111 地址,我的浏览器得到的是下面的响应,它是XML格式的,正是我要达到的目标。
任何意见,建议,和批评都很欢迎。
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。 2KB翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。2KB项目(www.2kb.com,源码交易平台),提供担保交易、源码交易、虚拟商品、在家创业、在线创业、任务交易、网站设计、软件设计、网络兼职、站长交易、域名交易、链接买卖、网站交易、广告买卖、站长培训、建站美工等服务