自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF Web API輕松實現(xiàn)REST

開發(fā) 前端
WCF Web API 是 “First-class programming model for HTTP in WCF”,而 HTTP 是 Web 世界的通行證,Web API 可以讓我們更輕松地暢游于 Web 編程世界。

先體驗一下,如果沒有 WCF Web API,直接用 WCF 實現(xiàn) REST 有多麻煩:

1. 創(chuàng)建 WCF 服務(ServiceContract)。

2. 創(chuàng)建 .svc 文件指向該 WCF 服務。

3. 在 web.config 中添加<service>/<endpoint>  配置,并將 binding 設置為 webHttpBinding,示例配置如下:

  1. <services> 
  2.     <service name="CNBlogs.OpenAPI.Service.NewsRestService"> 
  3.     <endpoint binding="webHttpBinding"   
  4.                 contract="CNBlogs.OpenAPI.Service.INewsRestService"   
  5.                 behaviorConfiguration="RESTFul" /> 
  6.     </service> 
  7. </services> 

4. 在 web.config 中添加 <endpointBehaviors>/<behavior> 配置,并在其中添加<webHttp />,在第3步的配置中添加該behaviorConfiguration(比如上面代碼中的behaviorConfiguration="RESTFul"),示例配置如下:

  1. <endpointBehaviors> 
  2.     <behavior name="RESTFul"> 
  3.         <webHttp /> 
  4.     </behavior> 
  5. </endpointBehaviors> 

5. 在 OperationContract 方法上增加 WebInvoke 屬性,示例代碼如下:

  1. [OperationContract]  
  2. [WebInvoke(Method = "GET",  
  3.     UriTemplate = "News/Recent/{itemcount}",  
  4.     ResponseFormat = WebMessageFormat.Xml)  
  5. ]          
  6. IQueryable<NewsItem> GetRecentNews(int itemcount); 

上面的5步已經(jīng)夠麻煩了。開始以為到此就可以收工了,哪知不運行不知道,一運行嚇一跳:

  1. Operation 'GetRecentNews' in contract 'INewsRestService' has a path variable named 'itemcount' which does not have type 'string'.    
  2. Variables for UriTemplate path segments must have type 'string'. 

參數(shù)竟然不能用int類型,必須要用 string。只能望 WCF 心嘆,不得不進入第6步。

6. 將 int 改為 string
 

  1. IQueryable<NewsItem> GetRecentNews(string itemcount); 

這是純 WCF 實現(xiàn) REST 的表演節(jié)目,節(jié)目名稱叫“ WCF 實現(xiàn) REST 六步走”,表演得分6分。

接下來,我們看看 WCF Web API 的表演

(如果不知道 WCF Web API 是何方神圣,請看演員介紹http://wcf.codeplex.com/wikipage?title=WCF%20HTTP)

1. 在提供 REST 服務的方法上增加 [WebGet(UriTemplate = "")] 屬性,示例代碼如下:

  1. [ServiceContract]  
  2. public class NewsRestService  
  3. {  
  4.     [WebGet(UriTemplate = "{itemcount}")]  
  5.     public IQueryable<NewsItem> GetRecentNews(int itemcount)  
  6.     {   
  7.         return newsList.AsQueryable();  
  8.     }   

2. 在 Global.asax 的 Application_Start 中添加路由,示例代碼如下:

  1. protected void Application_Start(object sender, EventArgs e)  
  2. {  
  3.     var config = new HttpConfiguration() ;  
  4.     RouteTable.Routes.Add(new ServiceRoute("news/recent",  
  5.         new HttpServiceHostFactory { Configuration = config },  
  6.         typeof(NewsRestService)));  

注:需要通過 NuGet 添加對 WebApi.All 的引用。news/recent 就是 REST 訪問網(wǎng)址。

收工!只需兩步就實現(xiàn) REST,WCF Web API 的表演得分2分。

2 : 6,WCF Web API 大獲全勝?。ǚ謹?shù)少的怎么反而獲勝?這是代碼世界,不是現(xiàn)實世界,程序員說了算,誰的代碼少,誰就獲勝)

小結(jié)
WCF Web API 是 “First-class programming model for HTTP in WCF”,而 HTTP 是 Web 世界的通行證,Web API 可以讓我們更輕松地暢游于 Web 編程世界。

實現(xiàn) REST 只是 WCF Web API 小試牛刀,我們還可以不用修改任何服務端代碼,只改變客戶端請求的方式,就可以返回不現(xiàn)類型的數(shù)據(jù)。
比如:

1)將 HTTP Header 中的 Accept 改為 “application/json”,返回的就是 JSON 數(shù)據(jù)。

2)通過 Url 參數(shù)發(fā)起 OData 查詢(比如“?$top=4&$orderby=Title” ),服務器收到請求后,會對返回結(jié)果進行 LINQ 查詢(因此示例代碼中的返回值類型是IQueryable<NewsItem>)。

原文:http://www.cnblogs.com/dudu/archive/2011/10/27/wcf_web_api.html

【編輯推薦】

  1. 百度地圖API之如何制作公交導航
  2. 走進Windows線程同步API
  3. 谷歌拼音輸入法擴展API開發(fā)指南
  4. 新浪微博API開發(fā)簡介之用戶授權(PHP基礎篇)
  5. 百度地圖API開發(fā)指南
責任編輯:陳貽新 來源: dudu的博客
相關推薦

2009-11-09 10:35:10

WCF REST服務

2010-10-28 09:56:53

Web APIRESTSOAP

2011-09-23 09:55:15

WCF Web API

2010-02-25 16:07:28

WCF REST

2009-11-06 14:40:34

WCF REST架構

2009-05-18 09:21:46

XMLRIAREST

2022-03-29 09:00:00

Angular框架REST API

2010-02-24 09:18:49

WCF Adapter

2009-07-22 14:50:50

ibmdwWeb2.0Twitter

2025-04-18 08:55:47

2009-11-09 15:28:04

WCF知識結(jié)構

2024-01-09 09:09:45

RESTGraphQL

2023-01-10 09:48:03

RESTAPIJersey

2009-09-22 11:49:34

ibmdwREST

2014-06-09 15:29:13

OData v4.0

2011-04-28 09:23:36

REST

2009-11-09 10:43:51

WCF Web編程模型

2022-02-10 23:38:23

API架構設計

2009-12-08 18:31:58

WCF WEB

2009-07-29 17:45:09

ibmdwWebREST
點贊
收藏

51CTO技術棧公眾號