Mar 4, 2010

Create a WCF RESTful service in Visual Studio 2010

Visual Studio 2010 (RC) doesn't have a template specifically for RESTful service. The WCF service template will create a WCF SOAP service, but it can be converted into REST service as follow:



Edit web.config:



The web.config entities for REST is quite different from those for SOAP, but very similar to those for Ajax-enabled WCF Service, the only difference is, Ajax-enabled WCF has "enableWebScript":








Add WebGet attribute to all methods in contract interface, like:


[WebGet(UriTemplate = "Hello", ResponseFormat = WebMessageFormat.Json)]



Add this attribute for REST implementation class:


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]


Test in Fiddler:

By now, it is a functioning WCF RESTful service, and response json format data. You can test it in Fiddler.


Great References:
http://www.ajaxlines.com/ajax/stuff/article/return_json_from_ajaxenabled_wcf_service.php
http://www.robbagby.com/rest/rest-in-wcf-blog-series-index/