Creating a REST Service with WCF and Windsor
Following on from my previous post, here is a short post showing how quickly you can create a REST based service using the WCF Facility for Castle Windsor. We will use the same service contract and implementation as the last post, with some very minor changes (only one, actually). The list of steps to expose our service from the previous post with a REST endpoint consists of:
- Create a new .svc file for the second endpoint.
- Add an attribute to the operation contract to specify what HTTP verbs are allowed.
<%@ ServiceHost Service=“my service” Factory=“Castle.Facilities.WcfIntegration.WindsorServiceHostFactory`1[[Castle.Facilities.WcfIntegration.Rest.RestServiceModel, Castle.Facilities.WcfIntegration]], Castle.Facilities.WcfIntegration” %>All we have done here is replace our factory with the REST aware service model from the WCF Facility. We are using the Service attribute to point to the same component as the SOAP endpoing.
And the new service contract:We’ve added the System.ServiceModel.Web attribute [WebGet] to allow HTTP GET to this endpoint. Now, our operation is availalbe as a REST endpoint, using a URL like so:
<string>A WCF Facility Service says I am RESTful</string>
It’s likely that you wouldn’t want applicaiton/xml to be the default return content-type, but the point of this post is not to show a best practice with REST, but to show how bleeding easy it is to expose a REST endpoint. We are also now exposing TWO endpoints (SOAP and REST) with a singular service implementation, a detail worthy of note. In the real world, you would likely have a service manager that registered service implementations and formatters, allowing the clients to specify a return type (XML, JSON, etc) and amending the content-type accordingly.
This, however, is my blog and could not be farther from the real world. ;)