The .Net framework has a number of technologies that allow you to create HTTP services such as Web Service, WCF and now Web API. There are a lot of articles over the internet which may describe to whom you should use. Now a days, you have a lot of choices to build HTTP services on .NET framework. In this article, I would like to share my opinion with you over Web Service, WCF and now Web API. For more information about Web API refers What is Web API and why to use it ?.
Web Service
- It is based on SOAP and return data in XML form.
- It support only HTTP protocol.
- It is not open source but can be consumed by any client that understands xml.
- It can be hosted only on IIS.
WCF
- It is also based on SOAP and return data in XML form.
- It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
- The main issue with WCF is, its tedious and extensive configuration.
- It can be hosted with in the applicaion or on IIS or using window service.
WCF Rest
- To use WCF as WCF Rest service you have to enable webHttpBindings.
- It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
- To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
- Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
- It support XML, JSON and ATOM data format.
Web API
- This is the new framework for building HTTP services with easy and simple way.
- Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
- Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats)
- It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
- It can be hosted with in the application or on IIS.
- It is light weight architecture and good for devices which have limited bandwidth like smart phones.
- Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.
To whom choose between WCF or WEB API
- Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication etc.
- Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable.
- Choose Web API when you want to create a resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).
-
Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.
http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-REST-and-Web-Service.html
WCF:基于RPC(遠端過程調用(Remote Procedure Call) )機制
Web API:一個基于HTTP的ASP.NET
WebService:基于Web的服務
在開發實踐中如何進行選擇?
可以參照知名網際網路企業,無論是google,facebook,baidu,新浪還是騰訊。他們對外開放的接口都是基于Http的Web API,在服務内部架構都是基于SOA架構設計的,通訊機制都是采用RPC機制的,例如Google Protocol Buffers ,Facebook thift。 我們完全也可以這樣搭配,在内部通訊采用WCF + Protobuf-NET,參看《WCF服務上應用protobuf》,對外的服務采用ASP.NET WEB API。WCF的 TCP、Named Pipes,甚至UDP(在WCF 4.5中)綁定的性能要比HTTP強很多倍,這裡有一個幾年前的微軟的測試報告《WCF 性能基準報告》,對外提供的服務采用Web API同時也是一個業界标準問題,用WebAPI就很容易的跨越ios,android,wp等移動終端平台,同時有很成熟的OAuth 解決安全問題。
當你遇到以下這些情況的時候,就可以考慮使用Web API了。
- 需要Web Service但是不需要SOAP
- 需要在已有的WCF服務基礎上建立non-soap-based http服務
- 隻想釋出一些簡單的Http服務,不想使用相對複雜的WCF配置
- 釋出的服務可能會被帶寬受限的裝置通路
- 希望使用開源架構,關鍵時候可以自己調試或者自定義一下架構
Web API的主要功能
1. 支援基于Http verb (GET, POST, PUT, DELETE)的CRUD (create, retrieve, update, delete)操作
通過不同的http動作表達不同的含義,這樣就不需要暴露多個API來支援這些基本操作。
2. 請求的回複通過Http Status Code表達不同含義,并且用戶端可以通過Accept header來與伺服器協商格式,例如你希望伺服器傳回JSON格式還是XML格式。
3. 請求的回複格式支援 JSON,XML,并且可以擴充添加其他格式。
4. 原生支援OData。
5. 支援Self-host或者IIS host。
6. 支援大多數MVC功能,例如Routing/Controller/Action Result/Filter/Model Builder/IOC Container/Dependency Injection。
WebAPI vs MVC
- MVC主要用來建構網站,既關心資料也關心頁面展示,而Web API隻關注資料
- Web API支援格式協商,用戶端可以通過Accept header通知伺服器期望的格式
- Web API支援Self Host,MVC目前不支援
- Web API通過不同的http verb表達不同的動作(CRUD),MVC則通過Action名字表達動作
- Web API内建于ASP.NET System.Web.Http命名空間下,MVC位于System.Web.Mvc命名空間下,是以model binding/filter/routing等功能有所不同
- 最後,Web API非常适合建構移動用戶端服務
釋出服務在Web API和WCF之間該如何取舍呢?這裡提供些簡單地判斷規則,
- 如果服務需要支援One Way Messaging/Message Queue/Duplex Communication,選擇WCF
- 如果服務需要在TCP/Named Pipes/UDP (wcf 4.5),選擇WCF
- 如果服務需要在http協定上,并且希望利用http協定的各種功能,選擇Web API
- 如果服務需要被各種用戶端(特别是移動用戶端)調用,選擇Web API
作者:PEPE
出處:http://pepe.cnblogs.com/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。