5 simple steps to create your first RESTful service

By | September 22, 2013
RESTful services are those which follow the REST (Representational State Transfer) architectural style. Before implementing your first RESTful service, lets first understand the concept behind it. As we know that WCF allows us to make calls and exchange messages using SOAP over a variety of protocols i.e. HTTP, TCP, Named Pipes and MSMQ etc. In a scenario, if we are using SOAP over HTTP, we are just utilizing HTTP as a transport. But HTTP is much more than just a transport. So, when we talk about REST architectural style, it dictates that “Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, simply HTTP should be used for making calls”.

::::: Practical Guide To WCF RESTful Service :::::

WCF RESTful ServiceRESTful architecture use HTTP for all CRUD operations like (Read/Create/Update/Delete) using simple HTTP verbs like (GET, POST, PUT, and DELETE).It’s simple as well as lightweight. For the sake of simplicity, I am going to implement only a GET request for which service will return certain types of data (i.e. Product data) in XML format.

Following are 5 simple steps to create your first RESTful service that returns data in XML.

  • Create a WCF Service Project.
  • Preparing the data (e.g. Product) to return
  • Creating Service Contract
  • Implementing Service
  • Configure Service and Behavior
1. Create a WCF Service Project

  •  Open Visual Studio.
  •  From File -> New Project.  Select WCF from left and create a new WCF Service Application.

2. Preparing the data to return

  • Now add a class to newly created project. Name it to Products.cs.
Now this Products.cs file will contain two things.First one is Data Contract
public class Product
                 public int ProductId { get; set; }
                 public string Name { get; set; }
                 public string CategoryName { get; set; }
                 public int Price { get; set; }
Second one is a singleton implemented class that gets products data from a database and return list of products. In order to make it simple, we are preparing data inside this class instead of fetching from the database as follows:
public partial class Products
        private static readonly Products _instance = new Products();
        private Products() { }
       public static Products Instance
                get { return _instance; }
        public List ProductList
               get { return products; }
          private List<Product> products = new List<Product>()
                 new Product() { ProductId = 1, Name = “Product 1″, CategoryName = “Category 1″, Price=10},
                new Product() { ProductId = 1, Name = “Product 2″, CategoryName = “Category 2″, Price=5},
                new Product() { ProductId = 1, Name = “Product 3″, CategoryName = “Category 3″, Price=15},
                new Product() { ProductId = 1, Name = “Product 4″, CategoryName = “Category 1″, Price=9}

3. Creating Service Contract

Now add a new WCF Service to this project as follows:

It will add contract as well as service file to project. Following is the code for service contract i.e. IProductRESTService.cs.

public interface IProductRESTService
           [WebInvoke(Method = “GET“,
                                    ResponseFormat = WebMessageFormat.Xml,
                                   BodyStyle = WebMessageBodyStyle.Bare,
                                   UriTemplate = “GetProductList/“)]
           List GetProductList();

IProductRESTService contains only one method i.e. GetProductList. Important points to understand about this method is WebInvoke attribute parameters.

  • Method = “GET”, represents an HTTP GET request.
  • ResponseFormat = WebMessageFormat.Xml, response format will be XML here but we can return JSON as well by changing its value to WebMessageFormat.json.
  • BodyStyle = WebMessageBodyStyle.Wrapped, indicates both the request and response are wrapped.
  • UriTemplate = “GetProductList/”, it has two parts, URL path and query.

Don’t forget to add using System.ServiceModel.Web at top.

4. Implementing RESTful Service
In this step we are going to implement the service. Only one method GetProductList is defined in the contract, so implementing service class will be as follows:

public class ProductRESTService : IProductRESTService
         public List GetProductList()
                      return Products.Instance.ProductList;

5. Configure Service and Behavior
The last step is to configure the service and its behaviors using the configuration file. Following is the complete ServiceModel configuration settings.

        <service name=”MyRESTService.ProductRESTService” behaviorConfiguration=”serviceBehavior”>
                <endpoint address=””
              <behavior name=”serviceBehavior”>
                  <serviceMetadata httpGetEnabled=”true”/>
                       <serviceDebug includeExceptionDetailInFaults=”false”/>
              <behavior name=”web”>
   <serviceHostingEnvironment multipleSiteBindingsEnabled=”true” />
 webHTTPBinding is the binding used for RESTful services.
Now, everything about creating RESTful service is done. You can easily run and test it.

Right click ProductRESTService.svc file and click “View in Browser“. You will see the following screen, that means service is fine.

Just modify the URL in browser and add “GetProductList/” to it. So, this is the UriTemplete we defined as service contract method.

Hopefully, this simple WCF tutorial will be helpful for the readers. To keep the things simple, I restrict it to just getting records using HTTP GET verb. In my coming article, I’ll provide all CRUD (Create, Read, Update, Delete) operations using RESTful service.

Other Related Articles:

Top 10 Interview Questions and Answers Series:

  • Mohammad Farahani

    thanks a lot

  • bryan

    this dont work

    public List ProductList
    get { return products; }

    • http://www.webdevelopmenthelp.net/ WebdevTutorial

      Can you please explain what doesn’t work for you. What error you’re getting?

  • Shrikant More

    Nice explained…

  • http://www.digitalsagotharan.com Jegadeeswaran Natarajan

    Thanks Dude.

  • anil

    Give me one more table class like Order,
    Combining both Products and Orders classes data want to get,
    How can i do?
    please give that example

    • http://www.webdevelopmenthelp.net/ WebdevTutorial

      Dear Anil,
      I’ll do the above mentioned in later tutorial here. We come up with a complete example. Please revisit or just register by email.

    • Najam Khan

      Create a class as below format:
      public class PostAlbumListRequest
      public List Album { get; set; }
      public User User { get; set; }
      public string UserToken { get; set; }


      [WebInvoke(Method = “POST”, UriTemplate = “PostAlbum”,
      RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
      PostAlbumResponse PostAlbum(PostAlbumRequest request);

      var album = {
      AlbumName: “PowerAge”,
      Entered: “1/1/1977″
      var user = {
      Name: “Rick”
      var userToken = “sekkritt”;

      url: “http://localhost/Rest/RestServiceImpl.svc/PostAlbum”,
      type: “POST”,
      contentType: “application/json”,
      data: JSON.stringify({ Album: album, User: user, UserToken: userToken }),
      success: function (result) {

  • Alex Alonso

    Very helpful for WCF beginers!

  • Howard Payne


    Using VS 2013 Express
    Created WCF Service Application project
    Works great…


  • http://www.webdevelopmenthelp.net/ WebdevTutorial

    Let me clear one thing as I mentioned in post step 1. You will create a new project using template “WCF Service Application”. Later in Step 3, you right click your project and add a new “WCF Service” to it.

  • David Gray

    I’m using VS2012. The WCF tab when creating a new project has; WCF Service Library, WCF Service application, WCF workflow service application & sysndication service libray.

    This is using the .net framework 4.

  • http://www.webdevelopmenthelp.net/ WebdevTutorial

    WCF Service or WCF Service Application template are available. I am not sure which version of Visual Studio you are using…

  • Anonymous

    Just use WebApi…

  • Anonymous

    The code is missing
    [ServiceContract] attribute
    public interface IProductRESTService { [OperationContract

    • http://www.blogger.com/profile/10867674357364468276 Imran Ghani

      Yeah, I missed that. thanks.

  • http://www.google.co.in/ hi

    Metadata publishing for this service is currently disabled

    • http://www.blogger.com/profile/10867674357364468276 imranghani

      Please verify you follow the step 5. Please confirm following in your configuration file.
      serviceMetadata httpGetEnabled=”true”

  • http://www.blogger.com/profile/05375223819491246846 Fabian

    Using WebApi is way much easier!

    • http://www.blogger.com/profile/10867674357364468276 imranghani

      WebAPI is another way of doing the same. You can find almost same in Web API at http://www.webdevelopmenthelp.net/2013/09/ASP-Web-API-Service.html

    • Anonymous

      i agree that web api is easier but this can be helpful if your org dont have enough fundings to upgrade to iis 7 or iis 8.