Monday, February 20, 2012

WCF Web API to ASP.NET Web API

WCF Rest Starter Kit --> WCF REST (WebHttpBinding) --> WCF Web API  --> ASP.NET Web API (WCF to ASP.NET Migration!)

ASP.NET MVC 4 now includes ASP.NET Web API (single integrated web API framework), a framework for building and consuming HTTP services that can reach a broad range of clients including browsers, phones, and tablets.

Everything including technology has a start and an end. Including technology, it necessities evolution to survive. This evolution of Web API track from WCF to ASP.NET is good for below reasons: 

1.       This will stop the confusion among developers to use what ASP.NET MVC Controller (JSON data from controller) or WCF Rest API

2.       Moving to ASP.NET MVC enables to better code re-use, easier & maintenance. One can use the same model to expose your data as HTML, JSON, XML, ATOM, OData etc….No Need of one more WCF Layer.

3.       HTTPClient / System.Net.HTTP part of .NET Framework 4.5 would be better suitable part of ASP.NET rather than WCF. Web API name itself contains both Web & Services! And Http belongs to more of Web than Services.

4.       Remember one of the main goal for WCF is unification of Microsoft’s distributed computing technologies (ASMX, .NET Remoting, Enterprise Services, WSE, System.Messaging, System.Net etc) with SOAP/XML as base framework. WCF won’t support or scale HTTP to full extent.  

This is not end of WCF, but the scope of exposing data via WCF is reduced (Mostly SOAP). World is around REST now. In the competitive world, the system or technology that are easily maintainable, sustainable, easy reach or interoperable including browsers, phones (IPhone, Android, Windows Phone etc….), tablets (IPad) only survives.  Restful approach follows most of the things – Simple, easily maintainable, reach etc. With Web API moves into ASP.NET, overall it is good for end-users. ASP.NET Web API is great for building services that follow the RESTful way.

 Why HTTP?

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative systems. HTTP is not just for serving up web pages. HTTP is simple, flexible, stateless and ubiquitous.

HTTP provides the following nine methods or verbs that we can perform on a identified resource. 
  • HEAD – Similar to a GET request without the response body. This is useful for retrieving meta-information written in response headers.
  • GET  - Requests to just retrieve the specified resource.
  • POST – Client sends data (message body) to the server for an action .
  • PUT - Uploads or updates the resource.
  • DELETE Deletes the specified resource.
  • TRACE - allows the client to see what is being received at the other end of the request chain and use that data for testing or diagnostic information
  • OPTIONS Returns the HTTP methods that the server supports for specified URL.
  • PATCH Is used to apply partial modifications to a resource.
What is REST?
REST defines an architectural style for building services in the “Web” way. REST is not tied to any particular technology or platform – it’s just another way to design things to work like the Web. Again this is not something new. REST concept exists for a long time but not utilized to its full power, until now.

Operations in Restful services are supported using HTTP methods (Like GET, PUT, POST, or DELETE).

Definition:
"Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. The term Representational State Transfer (REST) was introduced and defined in 2000 by Roy Fielding[1][2] in his doctoral dissertation. Fielding is one of the principal authors of the Hypertext Transfer Protocol (HTTP) specification versions 1.0 and 1.1." - From wikipedia.org

Why REST Popular?
 


Is Web Popular?! ;)

Web - WWW, which is omnipresent everywhere now. Almost everyone knows about Web. RESTful architecture simply follows the tenets of the web which provides the same reach to people. I will give you a specific simple example - Just think of accessing a Service as below: You can access whatever the book detail of any author you want through HTTP GET itself! No Soap Messages, Proxy etc.


amazon.com/authors/{author}/{book}
Ex: amazon.com/authors/ArunGanesh/VS2010
amazon.com/authors/{author}?book={book}
Ex: amazon.com/authors/ArunGanesh?book=VS2010
Key Items to remember (REST):

  


1. In Web, every resource being referred by a unique identifier, also known as a universal resource identifier (URI).

URI / Segment of a URI map to Application Logic. In the above example you can see that the segment of the URL itself being mapped to an application logic.


2. Utilize

HTTP Verbs - HTTP GET, POST, PUT, DELETE etc.


3. Utilize HTTP Headers for representing what content type is required and services is returning. For instance, you can represent things like: languages, authentication, authorization, accepted encoding, char set, content type, length etc...

What ASP.NET Web API provides: 

The integrated stack supports the following features: 
  • Modern HTTP programming model
  • Full support for ASP.NET Routing
  • Format flexibility
  • HTTP Client integration
  • Enabled OAuth and OData support
  • Configuration simplification
  • Content negotiation and custom formatters
  • Model binding and validation
  • Filters
  • Query composition
  • Easy to unit test
  • Improved Inversion of Control (IoC) via DependencyResolver
  • Code-based configuration
  • Self-host

What’s the key difference: 

ASP.NET Web APIs are based on controllers, classes which derive from a new ApiController class.

WCF Web API
ASP.NET Web API
Service
Web API controller                  
Operation               
Action
Service contract
Not applicable
Endpoint
Not applicable
URI templates
ASP.NET Routing
Message handlers
Same
Formatters
Same
Operation handlers
Filters, model binders


 

1 comment:

ASP.Net Migration said...

Great post about .net . Very interesting.