How to Create and consme RESTful WCF service
B Sahoo Posted On : 21/05/2013
About Author
Biswaranjan Sahoo have good working and teching experience in Microsoft technology.

Create and consme RESTful WCF service

All we know about WCF Services. Window Communication Foundation (WCF), code name Indigo is a programming platform and runtime system for building, configure and deploying network-distributed services.
You can say WCF is Microsoft’s unified programming model for building service oriented application with managed code. Interoperability is the fundamental characteristics of WCF. It extends the .NET Framework to enable developers to build secure and reliable transacted web services that integrate across platforms and interoperate with existing investments.

Also WCF supports concentrated computing where services have isolated consumers. Clients can consume multiple services as well as services can be consumed by multiple clients.

This is the basic things regarding WCF. But in this articles, I am going to tell you How we create and consume Restful services. Before that I want to give you idea about REST and RESTful services.

What is REST & RESTful Services

Representational State Transfer (REST) is introduced by Roy Fielding on 2000. It is an architectural style of large-scale networked software that takes advantage of the technologies and protocols of the World Wide Web. REST has emerged as a predominant web API design model.

The REST architectural style was developed by W3C Technical Architecture Group (TAG) in parallel with HTTP/1.1, based on the existing design of HTTP/1.0.The World Wide Web represents the largest implementation of a system conforming to the REST architectural style.REST-style architectures conventionally consist of clients and servers.

Clients initiate requests to servers. servers process requests and return appropriate responses. Requests and responses are built around the transfer of representations of resources. A resource can be essentially any coherent and meaningful concept that may be addressed. A representation of a resource is typically a document that captures the current or intended state of a resource.

REST (REpresentational State Transfer) is a simple stateless architecture that generally runs over HTTP. WCF Services which is developed using REST architectural style is known as RESTFul Services.

REST involves reading a designated Web page that contains an XML or JSON  file. The XML file describes and includes the desired content.

The REST style emphasizes that interactions between clients and services is enhanced by having a limited number of operations (verbs). Flexibility is provided by assigning resources (nouns) their own unique universal resource indicators (URIs). Because each verb has a specific meaning (GET, POST, PUT and DELETE), REST avoids ambiguity.

The main idea behind REST is that we should treat our distributed services as a resource and we should be able to use simple HTTP protocols to perform various operations on that resource. Every application has one moto, i.e insert/delete/update/retrieve information. That means CRUD operation.

The basic CRUD operations are mapped to the HTTP protocols in the following manner:

GET: This maps to the R(Retrieve) part of the CRUD operation. This will be used to retrieve the required data (representation of data) from the remote resource.
POST: This maps to the U(Update) part of the CRUD operation. This protocol will update the current representation of the data on the remote server.
PUT: This maps to the C(Create) part of the CRUD operation. This will create a new entry for the current data that is being sent to the server.
DELETE: This maps to the D(Delete) part of the CRUD operation. This will delete the specified data from the remote server.

 

Difference between WCF Services and RETful Services

WCF Services

RESTFul Services

Endpoints have to create for each network protocol

It can be connect over "Web" with HTTP request/response messages.

It works based on the Remote Procedural Call (RPC)

It is working with HTTP's uniform interface

Service Reference has to add in the client applications

No need to add the service reference in the client applications

Difference between Rest and Soap

SOAP

RESTFul Services

REST is an "architectural style" that basically exploits the existing technology and protocols of the Web, including HTTP (Hypertext Transfer Protocol) and XML and JSON.

 

REST is simpler to use than the well-known SOAP (Simple Object Access Protocol) approach, which requires writing or using a provided server program (to serve data) and a client program (to request data).

 

Step by Step Guide for creating a basic RESTFul Service

Step-I

Create a new WCF project using VS2010. For that you have to click on FILE -> NEW -> PROJECT. then select WCF Service Application

Step-II

Delete the existing IService1.cs & Service1.svc files and Add WCF Service item form Add New Item templet, named as MyFirstRESTService. It added IMyFirstRESTService &  MyFirstRESTService.cs. file to application.

Step-III

Now time came, to create Restful Service. As I earlier explained that Restful service can return data in XML and JSON format. set WebInvoke attribute  to operationcontract in Interface file. You can use WebGet attribute in place of WebInvoke. Both specifying that the method will be available through a REST call.

Through WebInvoke attribute, we can set Method type, ResponseFormat, BodyStyle and UriTemplet. The UriTemplate defines the access point, this being set to root.

 

Step-IV

Now open MyFirstRESTService.cs file and add your logic here or write code as per given below.

Step-V

Now let's move to service configuration part. There will be three basic parts of configurations file , we have to change.

  1.  Create End Point with binding ="webHttpBinding"
  2. Create endpoint behavior with <webHttp/> and gave a suitable name to that behavior.
  3. Assign the behavior name to bhaviorConfiguration of endpoint.

Now our Restful service is ready for test.
open the browser , paste the service url and check the output. you got below result.

WebGet and WebInvoke

WebGet operation is logically receiving the information from a service operation & it can be called by the REST programming model. The WebGet attribute is applied to a service operation in addition to the OperationContract and associates the operation with a UriTemplate as well as the HTTP protocol Get verb.

WebInvoke operation logically raises a service option & it can be called by the REST programming model. The WebInvoke attribute is applied to a service operation in addition to the OperationContract and associates the operation with a UriTemplate as well as an underlying transport verb that represents an invocation (for example, HTTP POST, PUT, or DELETE). WebInvoke has a property called Method, it allows specifying different types of HTTP methods (POST, PUT or DELETE), and by default Method is POST.

Conclusion

I hope this article helps you to understand basic concept of RESTFul service  Please not forget to post comments.

Reference

  1. http://www.codeproject.com/Articles/255684/Create-and-Consume-RESTFul-Service-in-NET-Framework
  2. http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
  3. http://blogs.msdn.com/b/kaevans/archive/2008/04/03/creating-restful-services-using-wcf.aspx
  4. http://en.wikipedia.org/wiki/Representational_state_transfer

Post your Suggetion or Comment
Name :
Email Address :
Comment :