ASP.NET Web API which comes with ASP .Net MVC 4.0 is a framework which helps us to build HTTP web services. It provides a simple way to build and expose REST-based data services. WebAPI is an ideal platform for building pure HTTP based services where the request and response happens with HTTP protocol. The client can make a GET, PUT, POST, and DELETE request and get the WebAPI response appropriately. Its main goal has been to increase the reach to a broad range of clients, including browsers and mobile devices.
With WebAPI content negotiation, we can return data based on the client requests. which means that if the client is requesting the data to be returned as JSON or XML, the WebAPI framework deals with the request type and returns the data appropriately based on the media type.
Web API is very similar to ASP.NET MVC in that it leverages some of the same core concepts, such as routing, controllers, and even controller action results. It uses these concepts, however, to support a very different set of scenarios: scenarios that involve working with data as opposed to generating HTML markup.
Why do you need Web API when you already have WCF?
The following table describes the major features of each technology –
ASP.NET Web API
|Supports HTTP, TCP, UDP, and custom transport protocols||Supports HTTP only.|
|Support multiple encodings (Text, MTOM, and Binary)||Support wide variety of media types including XML, JSON etc.|
|Supports building services with WS-* standards like Reliable Messaging, Transactions, Message Security.||Uses basic protocol and formats such as HTTP, WebSockets, SSL, JQuery, JSON, and XML. No support for higher level protocols such as Reliable Messaging or Transactions.|
|Supports Request-Reply, One Way, and Duplex message exchange patterns.||HTTP is request/response but additional patterns can be supported through SignalR and WebSockets integration.|
|WCF SOAP services can be described in WSDL allowing automated tools to generate client proxies even for services with complex schemas.||There is a variety of ways to describe a Web API ranging from auto-generated HTML help page describing snippets to structured metadata for OData integrated APIs.|
|Ships with the .NET framework.||Ships with .NET framework but is open-source and is also available out-of-band as independent download.|
WCF is very powerful, flexible, secure, and robust . The only drawback is that very few platforms are implementing those protocols. Even the ones that did, have certain differences in their implementation that prevent them from being truly interoperable across a wide variety of platforms. So if you really want services that can be exposed and easily consumed from many platforms, specially mobile devices such as phones and tablets as well as browsers, you need a simple services which support different message formats other than SOAP.
WCF uses HTTP only as a transport protocol, the same way it uses MSMQ, TCP, Named Pipes etc. where as HTTP itself is capable of handling much deeper semantics .HTTP is a lot more than a transport protocol. It is an application-level protocol. Although many platforms know how to use SOAP, many more platforms know how to use HTTP . ASP.NET Web APIs wants to leverage this .
Using the ASP.NET Web API it is very simple to create a service that exposes some functionality and data over HTTP. It should also be noted that ASP.NET Web API cannot do everything that WCF can do . It does not support the full range of security, transactions reliability, callbacks etc that are the powerful features of WCF( due to its supported protocols) . But the fact is that we do not need all of these every time.
ASP.NET Web API supports a number of other important capabilities for Web APIs and REST services including the ability to support any format in the HTTP body messages, and the ability to support content negotiation, the ability to map resource Uris into handling methods, automatically convert and pass parameters and return types to methods in a strongly typed way . Caching GET requests is a huge part of HTTP and of scaling any web site/service. One of SOAPs biggest disadvantages is that it relies exclusively on HTTP POST when using HTTP as a transport and so cannot take advantage of caching of requests. Another drawback of WCF is that it does overuse of configuration and attributes where as the ASP.NET Web API works without any configuration, you need no web.config code for it. It uses convention over configuration to eliminate the need of manual configuration .
So you can think of ASP.NET WebAPI as a simpler more lightweight alternative to WCF. ASP.NET WebAPI services operate over HTTP and are best suited to simpler tasks, whereas WCF is more versatile in the protocols that it can communicate with .It is a tradeoff when you are planning to choose between these two. Do you want a simple and open solution which is going for reach or a closed solution for richness and more flexibility.
Following are some points which can help you decide which one to choose among ASP .Net Web API or WCF based on your requirements:
- If your intention is to create services that can use fast transport channels when available, such as TCP, Named Pipes etc and you also want to support HTTP when all other transports are unavailable, then you should go for WCF.
- If you want to create services that support special scenarios like,message queues,one way messaging, duplex communication etc, then you’re better off choosing WCF.
- If you want to create resource-oriented services over HTTP that can use the full features of HTTP ,define cache control for browsers, use URI templates to include Task URIs in your responses , pass various content types such as images, documents, HTML pages etc. then the best choice for you will be the ASP.NET Web APIs.