WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Thursday, March 15, 2012

WCF - REST Endpoints JSON

With more public facing websites providing access to their data, it is quite necessary for the API to be of KISS [Keep it simple Stupid] by design. Long gone are the days with services being SOAP and XML based. REST [Representational State Transfer] based endpoints is the way to go now.a.days. REST based urls are both SEO friendly and much for cleaner URI and easier to remember. When it comes to API design, a REST based endpoint provides a simple and clean URI for it to be consumed.

In .NET world, REST based endpoints can easily be created in an ASP.NET MVC application either by using Routes or by URL rewriting. But in the case of WCF (Windows communication Foundation) REST based endpoints are not provided out-of-the-box. To make this happen we can either configure WCF manually (this article) or we could use frameworks like WCF Web API (which is now called ASP.NET WebAPI and would be part of ASP.NET MVC 4.0) and ServiceStack.NET. The idea behind both the frameworks (SS.NET & WebAPI) is to provide REST based out of the box support for XML, JSON and ODATA (in webapi).

To configure WCF to provide REST based enpoint we need to first enable “ASPNETCompatibility” and have the REST endpoint bind to “webHttpBinding”. In the WCF web.config, following changes would be needed:



Step 1 is to enable aspNETCompatibility we need modify the “serviceHostingEnvironment” to turn on compatibility.


Step 2 is to create a endpoint behaviour to enable http based GET/POST requests. We create the following behavior and provide it with a name for reference:



Step 3 is to hook up the webservice to this behavior either at its default URI location or a custom URI location. Following is a json endpoint for the service:



For WCF Service to allow http based requests we need to bind the service to “webHttpBinding”. webHttpBinding is used to configure endpoints for Web services that are exposed through HTTP requests instead of SOAP messages. Do keep in mind, the contract is the Interface namespace of the service being exposed.

Step 4 is now to enable ASPNEtCOmpatibility in the Service Class



Note: ASpNETCompatibilityRequirements attribute is based of “System.ServiceModel.Activation” namespace.

Step 5 is offcourse to let the service to know if the output is a JSON or XML response. In the service interface, the methods attributes needs to be defined:



Note that Request and Response format can be JSON or XML and also Method could be either POST or GET. URI Template is the method name that will be exposed thru the service.

You are DONE!

You can access the service using the SVC URI like http://localhost/Service1.svc/json/{method}

NOTE: If you would like to add a SOAP based URI endpoint to provide a WebApi style service interface, you can add the following endpoint to the service section



JSON endpoint : http://localhost/Service1.svc/json/{method}
SOAP endpoint: http://localhost/Service1.svc/soap/
WSDL: http://localhost/Service1.svc?wsdl

Labels: , ,