ASP.Net Web API is a framework which is used to build API (Http
Services) on top of the .Net framework. The most common use case for using Web
API is for building RESTFul services. These services can then be consumed by
broad range of clients like browsers, mobile applications, desktop
applications, IOTs. The term IOT stands for Internet of Things. IOTs are the
objects or devices that have an IP address and can communicate over the
internet with other internet enabled devices and objects. E.g. Security
Systems, electronic devices like desktop, laptop, smart phones, etc. Though ASP
.Net Web API is widely used to create RESTFul services, it can also be used to
create services which are not RESTFul. In short, it does not dictate any
architectural style for creating services.
What are RESTFul services?
REST stands for Representational State Transfer, first introduced
by Roy Fielding. It is an architectural pattern for creating API that uses Http
as its underlined communication mechanism. REST architectural pattern specifies
set of constraints that a system should adhere to.
REST constraints:
1.
Client Server:
Client sends a request, server send a
response. This separation of concerns supports the independent evolution of
client side logic and server side logic.
2.
Stateless:
The communication between the client and
the server should be stateless, which means we should not be storing anything
on the server related to the client between requests. The request from the
client should contain all the necessary information for the server to process
that request. This ensures that each request can be treated independently by the
server.
3.
Cacheable:
Sub data provided by the server like list
of products, list of department, does not change that often. This constraint
tells the client that how long the data is good for, so the client does not
have to come back to server over and over again for the same data. Caching
avoids unnecessary processing and increased the performance.
4.
Uniform Interface:
It defines an interface between client and a
server. To understand uniform interface, we must understand what a resource is
and the Http verbs GET, POST, PUT and DELETE. In context of REST APIs,
resources typically represent data entities. Product, Employee, Department, etc
are all resources. The Http verb GET, PUT, POST, DELETE sent with each request
tells the APIs what to do with the resource. Each resource is indentified by a
specific Uri.
Let’s look at the below table.
Resource here is Customer. So typical Uri
would be http://sitename/Customers.
This Uri with the verb GET, is going to get us list of Customers.
http://sitename/Customer /1. This Uri with the verb GET would get us a customer with id = 1.
Another concept related to Uniform Interface is HATEOS (Hypermedia as the Engine of Application State). It means that in each request, there will be a set of hyperlinks that lets you know what other actions can be performed on the resource.
Let’s look at the below table.
Resource
|
Verb
|
Outcome
|
/Customers
|
GET
|
Gets list of customers
|
/Customer/1
|
GET
|
Gets a customer with id = 1
|
/Customers
|
POST
|
Creates a new customer
|
/Customer/1
|
PUT
|
Updates a customer with id = 1
|
/Customer/1
|
DELETE
|
Deletes a customer with id = 1
|
http://sitename/Customer /1. This Uri with the verb GET would get us a customer with id = 1.
http://sitename/Customers.
This Uri with the verb POST would create a new customer.
And you are smart enough to understand the
functions of remaining verbs.
Another concept related to Uniform Interface is HATEOS (Hypermedia as the Engine of Application State). It means that in each request, there will be a set of hyperlinks that lets you know what other actions can be performed on the resource.
When to choose WCF services?
We can also use WCF service to convert into a RESTFul service
but it requires a lot of configuration. The more natural choice to create a
RESTFul service would be a Web API which is specifically created to create a
RESTFul service.
a.
WCF is used when you want to create a service
which is transport/protocol independent. A single service with multiple
endpoints. Suppose you have two clients, a java client and a .Net client. Java
client wants message in XML format over Http protocol and .Net client wants
binary message over tcp protocol. We can simply define two endpoints for two
clients and our work is done.
b.
If you have an existing SOAP service but want to
add REST to reach more clients, use WCF service.
c.
If you have limitation of using .Net framework
3.5, use WCF service.
No comments:
Post a Comment