Restful web services using C# (Part – 37)

What is Rest?

Representational state transfer(REST) is an abstraction of architecture of world wide web. REST is an architectural style to design networked application.REST makes communication between remote computers easy by using the simple HTTP protocol which support for CRUD (Create, Read, Update, and Delete) operations on the server

Why Rest?

For an example, you have to build a Web Service which should be cross functional and should be able to be consumed by any client. Traditional Web Services in .NET have to be consumed using a proxy. What if the client does not know how to create a proxy?

RESTful web services uses HTTP directly:

  • No RPC layer like SOAP or XML-RPC
    • it uses the infrastructure of the Web as it was designed to communicate between client and server
    • Designed to scale
    • Little or no middleware required
  • Main abstraction is Resource not type
    • Resources have uniform interfaces,no negotiation of type interface
    • Servers are stateless

REST is layered:Interactions are uniform across network layers

  • Clients don’t know whether they are communicating with end servers,proxies or gateways
  • Components can be independently deployed to improve performance and enforce security

REST constrains itself to a uniform interface

  • All operations have well known semantics
  • Most operations are either safe or idempotent
    • Safe means the operation has no side effects
    • Idempotent means the operation can be performed multiple times without changing end result
  • Responses state whether the resources are cacheable.

Difference between Rest and SOAP services:

  • REST is simpler to program than SOAP
    • Retrieval consists of a URL(GET)
    • Creation consists of a URL(POST)
    • Update and Delete also simple (PUT,DELETE)
  • SOAP semantics are opaque, so it bypasses the features of layered internet
  • REST is lightweight on the client side
    • you need to write a little bit of script codeand you are off and running
    • Javascript frameworks make it browser independent and easy to integrate
  • REST is lightweight in server side
    • you need to write a little bit of script codeand you are off and running
    • Frameworks support the web servers

Difference between SOAP and Rest services:

  • It is easy to get REST wrong
  • SOAP tool suite is bigger
  • SOAP offers type safe and formal contracts
  • Web services stack includes integrated middleware standards those are useful for security,reliable messaging etc..

What is in a RESTful webservice design?

  • A set of resources
  • URLS for your resources
  • The operations you wanted to perform on those resources
  • The set of representations for those resources
    • from server to clients
    • from clients to server

Uniform Interface style:

  • Operations on the base URI affect the set of resources as a whole
    • GET lists them
    • POST adds a new resource to the set
    • DELETE deletes the whole set
    • PUT replaces the set with a new set
  • Operations on an ID-qualified URI affect an individual resource
    • GET retrieves it
    • DELETE destroys it
    • PUT replaces it or create if doesnot exists

Resource URLS:

  • The service has a base URI that represents whole set of resources
    • ex: TechKatak.com/posts
  • The base uri can be qualified with an identifier specifying an individual resource
    • ex: TechKatak.com/posts/1053
  • RESTful services are hierarchical, resources offered by one service can contain more service 

Create a basic Restful service in C#:

  • Visual studio –>File–>New–>project–>WCF–>WCF Service Application and rename it to BasicRestService
  • Delete all default code from service1.svc.cs and also from contract
  • Code for iService1.cs:
  • Code for Service1.svc.cs
  • Code for Service1.svc–>View markup
  • Comment this line from config file

Run the application to see output.

© 2015, www.techkatak.com. All rights reserved.