What is REST?

Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. As such, it is not just a method for building "web services." The terms "representational state transfer" and "REST" were introduced in 2000 in the doctoral dissertation of Roy Fielding,[1] one of the principal authors of the Hypertext Transfer Protocol (HTTP) specification.

REST is a method of managing resources over HTTP. There are a number of methods of interacting with resources via REST.

POST Create
GET Read
PUT Update, Create

Systems which follow Fielding’s REST principles are often referred to as "RESTful".


Our Basic Service

For the purpose of this post we are going to create a service that returns a product based on the requested product id.

Start by adding a new WCF service to your project. We’ll call it Products.

Change the interface that is generated to look like this:

   1: [ServiceContract]

   2: public interface IProducts 

   3: {

   4:     [OperationContract]

   5:     [WebInvoke(Method = "GET",

   6:      ResponseFormat = WebMessageFormat.Json,

   7:      BodyStyle = WebMessageBodyStyle.Wrapped,

   8:      UriTemplate = "get/{id}")]

   9:     string Get(string id);

  10: }

In the above code we use the ServiceContract attribute to say that this interface is defining a service and makes the interface visible to WCF clients. We then define a method called Get. We mark this method with 2 attributes. First we use OperationContract. This just lets WCF know that this is a method of the service. Next we use WebInvoke. This means that the method is an invoke operation and maps it to an HTTP verb. In this case we have mapped it to the GET verb, specified we want our response to be JSON, and defined what to an URI will look like. The UriTemplate maps our id value to out method parameter.

   1: public class Products : IProducts 

   2: {

   3:     public string Get(string id) 

   4:     {

   5:         return "You requested product " + id;

   6:     }

   7: }

This next part is simple. We are just implementing our interface. Our method is pretty simple for demonstration purposes. We’ll just output the id value that we receive.

This next part that you need in your Web.config is important for your service to work. This maps the incoming requests to your services.


Calling Our API

So, now that our service is finished it is easy to call like http://yourdomain.com/Products.svc/get/123456. This is telling our service we want to use the Get method of the Products service and we are looking for product id 123456.

This will give us a response of:

{"GetResult":""You requested product 123456"}


Wrap Up

This is just a simple example of how to setup a RESTful web service quickly and easily by letting WCF take care of the plumbing and heavy lifting.

Stay tuned for more great posts from Dev102.com by grabbing the RSS feed. You can also see more posts I have written on my blog or grab my RSS feed.

Tags :

6 Responses to “Creating A RESTful API With WCF”

  1. sboulay

    Said on July 17, 2009 :

    how do you get ride of the .svc extension in the url?

  2. aidos

    Said on July 24, 2009 :

    “This next part that you need in your Web.config is important for your service to work. This maps the incoming requests to your services.”

    Could you provide some more detail here? This is the part that I always feel is missing from REST-using-WCF articles – it concentrates on the relatively easy part of defining the service and skips over the more complicated issue of getting it registered in the Web.config file.


  3. AnHund

    Said on March 1, 2010 :

    The WebInvoke attribute is found by adding a reference to “System.ServiceModel.Web.dll” and adding “using System.ServiceModel.Web” in the code.

  4. Shut Down the Internet

    Said on June 24, 2010 :

    cool, using WCF with REST is an interesting

2 Trackback(s)

  1. Jul 17, 2009: DotNetBurner - WCF
  2. Jul 24, 2009: Scott Banwart’s Blog » Blog Archive » Distributed Weekly 7

Post a Comment