In designing software there is a fundamental trade-off between simplicity and power. Great products have just the right mix of the two. When it comes to REST the team has been grappling with this balance. If you have seen the WCF REST Starter Kit then you have seen a feature packed solution for building RESTful services. However, some people say they want more simplicity. Today I’m going to show you the easiest way to build a RESTful service. For the scenario let’s imagine I’m building a website for a conference. I want the website to provide a REST service that returns one or all conference sessions.
The resource is just a class that will be serialized to return the data to your callers. It is typically a very simple class with public properties.
public class ConferenceSession { public string ID {get;set;} public string Title { get; set; } }
public class ConferenceSession
{
public string ID {get;set;}
public string Title { get; set; }
}
Notice this is a very simple class with no attributes, interfaces or base class required.
It makes sense to keep this simple. We are going to provide a service that returns one or all conference sessions
Since this model is so common, I created a generic contract that you can use over and over again if you use this information model.
using System.ServiceModel; using System.ServiceModel.Web; namespace EasyREST { [ServiceContract] public interface IEasyREST<T> where T:class { [OperationContract] [WebGet(UriTemplate="/{id}")] T GetItem(string id); [OperationContract] [WebGet(UriTemplate = "/")] T[] GetItems(); } }
using System.ServiceModel;
using System.ServiceModel.Web;
namespace EasyREST
[ServiceContract]
public interface IEasyREST<T> where T:class
[OperationContract]
[WebGet(UriTemplate="/{id}")]
T GetItem(string id);
[WebGet(UriTemplate = "/")]
T[] GetItems();
Now I can create a service for my website. These are the manual steps, you can create a template that already has most of this done if you want.
<%@ ServiceHost Language="C#" Debug="true" Service="EasyREST.Conference" CodeBehind="Conference.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
<%@ ServiceHost
Language="C#"
Debug="true"
Service="EasyREST.Conference"
CodeBehind="Conference.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
public class Conference : IEasyREST<ConferenceSession> { public ConferenceSession GetItem(string id) { ConferenceSession session = ConferenceGateway.GetSession(id); if (session == null) WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound(); return session; } public ConferenceSession[] GetItems() { return ConferenceGateway.GetSessions(); } }
public class Conference : IEasyREST<ConferenceSession>
public ConferenceSession GetItem(string id)
ConferenceSession session = ConferenceGateway.GetSession(id);
if (session == null)
WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
return session;
public ConferenceSession[] GetItems()
return ConferenceGateway.GetSessions();
This is a very simple reusable REST solution. The question is… is this enough? I suspect for a large number of RESTful services this will provide just what you are looking for. It’s easy to grasp and easy to use is it not?
I’d be interested in hearing what you think about it. I’ve added this project to the MSDN Code Gallery at http://code.msdn.microsoft.com/easyrest.
PingBack from http://www.clickandsolve.com/?p=14367
Thank you for submitting this cool story - Trackback from DotNetShoutout
But what's about UpdateItem(id, title)?
Most RESTful services are read only so this service does not include Add/Update/Delete methods although it would be trivial to do so.
#.think.in infoDose #19 (23rd Feb - 27th Feb)
Pick of the week: Paying Down Your Technical Debt General Leveraging ILMerge to Simplify Deployment and Your Users’ Experience : Daniel Cazzulino demonstrates how you can easily combine multiple assemblies into a single assembly. DDD Step By Step : Casey
Well, I tried something similar with normal SOAP services, I had a big problem: what does it happen when you want to expose two entities? It would be fantastic if I could mix various servicecontracts in the same WSDL.
You can surface only 1 resource per .SVC file. So for a conference website I might have a Sessions.svc file that exposes the session resource and a Speakers.svc file that exposes the speaker resource.