Click here to Skip to main content
11,928,985 members (49,520 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


69 bookmarked

How to create a JSON WCF RESTful Service in 60 seconds

, 10 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to expose JSON data over a RESTful interface.

WCF makes it very easy to expose JSON data over a RESTful interface, as long as you are aware of a couple of “gotchas” in advance.

This article will explain those to you, so you can focus on your business logic rather than configuration of your WCF services.

We start this example by creating a WCF Service Library project:


Next we need to add a reference to the System.ServiceModel.Web framework.  Right click on your project file and select Add Reference…


As this framework is not part of the .Net Framework 4 Client Profile, Visual Studio kindly informs us that it will update our target Framework to the full version of .Net Framework 4.  Click Yes to accept this change:


We are now ready to update our code.

Copy and paste the following code into the App.Config file:

<?xml version="1.0"?>
	  <service name="WcfJsonRestService.Service1">
		<endpoint address="http://localhost:8732/service1" 
		  <webHttp />
	<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

Notice the binding is set to webHttpBinding as opposed to the normal project template default of wsHttpBinding. 

The other important change is the addition of an endpointBehavior for WebHttp.

These two changes are required to enable JSON over REST for WCF.

Copy and paste the following code into the IService1 file:

using System.ServiceModel;

namespace WcfJsonRestService
    public interface IService1
        Person GetData(string id);

Notice we are accepting an “In” parameter for id of datatype string.  For this example we are returning a custom type of Person.

Copy and paste the following code into the Service1.cs file:

using System;
using System.ServiceModel.Web;

namespace WcfJsonRestService
    public class Service1 : IService1
        [WebInvoke(Method = "GET", 
                    ResponseFormat = WebMessageFormat.Json, 
                    UriTemplate = "data/{id}")]
        public Person GetData(string id)
            // lookup person with the requested id 
            return new Person()
                           Id = Convert.ToInt32(id), 
                           Name = "Leo Messi"

    public class Person
        public int Id { get; set; }
        public string Name { get; set; }

The key elements here are the attributes applied to the method.  We are enabling the method to be called over HTTP GET, returning the data in Json format and setting the Uri template to ensure we are using a RESTful interface.

To test your brand new service we will pass in the id value of 10 simply by opening your favourite browser and pasting in the following URL: 




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United Kingdom United Kingdom
Developer of .Net Enterprise solutions in the City of London

You may also be interested in...

Comments and Discussions

QuestionRemote Restful services are not working PinPopular
bindugadu7-Nov-12 4:18
memberbindugadu7-Nov-12 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 10 Mar 2011
Article Copyright 2011 by StevenHollidge
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid