How to create RESTful WCF Service ?

 

Hello friends,

Many times you heard about RESTful services. I know most of you pretty much aware also but let me Introduce in my way.

The first question  come to your mind which is

What is REST?

So here is my answer it is Representational State Transfer.

Its all about using HTTP protocol.With the help of this you can perform various action like PUT,GET,POST,DELETE etc.

Suppose you have a student database and want to pull a particular roll number’s record. then in that case you will do following thing
http://indiandotnet.in/GetData/Name

Where 123 is roll number. So with simple URL you can fetch the information similar way you can edit,delete and create student record also with similar type of URL(may be few parameters vary depending on your requirement)

How to create a RESTFUL WCF services ?

Creating RESTFUL WCF service is not a typical part Lets understand it by Step by Step Example

So lets start a new project WCF project as shown in below image i have added one project

 REST_Solution_1

Step 2:-

Now  i am adding two methods here which are  basically for showing two different format XML & JASON using REST WCF application. The important point

definedTwomethod

Step 3:-

Now to make this method as a RESTful service we need to add WebGET attribute as  i am dealing with only Fetching and GET method. as shown in above image.  I am using WebGet but you can try different method like, POST,DELETE,PUT using Web Invoke attribute.

To make this simple to understand i am using Student class with only two property first name & Last Name

Student_Class

Step 4: Below are the two methods in which from one method  we are responding XML & from one method we are returning the JASON.

Method_Definition

Step5:

Once step 4 is done we will configure most the most important part of RESTFUL WCF application which is binding , contract  information etc.

Capture

Once the above configuration is done just run the service.

lets run it with fiddler to understand it more.

When we  running the URL with GETData method as shown it below

  http://localhost:51550/Service1.svc/GetData/Name 

running_1

When we run it we get xml Response as shown below

Rest_Response_XML_2

Now we are running other method using JASON method (as shown in below method)

We will get following response.

Response_XML_3

So in this way we can create RESTFul application.

You can download  code from

Thanks & Best Regards,

Rajat Jaiswal

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s