REST Service with WCF – Testing with Fiddler

In this article I am explaining simple REST service which can accept HTTP GET Request.In response to this request service return data in XML format.You could use return type as Json format as well.This can easily set with WCF interface attribute as  ResponseFormat = WebMessageFormat.Xml.And if you associate your return type  with DataContract attribute System.Runtime.Serialization does the serialization for you.

1) Create Service Library Project

Choose service library Template project as shown below,

WCFLib

2) Define your Contract and your data model

Reference System.ServiceModel.Web will enbable the HTTP requests, by attaching Webget Abbribute to your Contract and also define the Response format and  UriTemplate which will be used for call the you method in the contract, which can be your method name as well.

Also define you data model class, attaching with Datacontract attribute will enable the runtime to serialize your object.

ContractAndService

3) Implement the contract method

It is just implement contract method in your service.Also attach the InstanceContextMode in the service behaviour attribute will you while hosting for example with console application.

Service

I have not done much change  in the app config for the service yet, you can use default service  configuration with webHttpBinding.

4) Hosting Your service with a Console application.

It time to Host your service, I am using simple hosting method which is useful debugging, Console application.

Reference the service library project and host the service as below.If you are running on Vista you might to run with administrator permission.

host

5) Make HTTP Request Using Fiddler Tool

Your service is running in this case at http://localhost:8732/RestService and you want to call HelloWorld method.You have used the UriTemplate as /Hello.

The URL for Get request is http://localhost:8732/RestService/Hello, use it the Fiddler tool as shown below.

fiddler

You can the out in the selected text.Similarly you can extend this and you can other HTTP methods POST,PUT.In that case you can use WebInvoke attribute for contract method.