Click here to Skip to main content
14,767,499 members
Articles » Platforms, Frameworks & Libraries » Windows Communication Foundation » General
Technical Blog
Posted 10 Mar 2011

Tagged as


84 bookmarked

How to create a JSON WCF RESTful Service in 60 seconds

Rate me:
Please Sign up or sign in to vote.
4.85/5 (56 votes)
10 Mar 2011CPOL
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

Comments and Discussions

QuestionJavaScript Client for this Service Pin
Jim Rolph10-Aug-18 14:43
MemberJim Rolph10-Aug-18 14:43 
QuestionWorks on localhost but not on server Pin
Member 117349026-Oct-17 3:30
MemberMember 117349026-Oct-17 3:30 
QuestionEasiest example that actually work! Pin
JoseLozano13-Apr-17 3:43
MemberJoseLozano13-Apr-17 3:43 
QuestionYou da man Mr. Steven Hollidge Pin
Eiinewton17-Jun-16 7:58
MemberEiinewton17-Jun-16 7:58 
GeneralMy vote of 4 Pin
Fawad Raza21-Dec-15 0:06
MemberFawad Raza21-Dec-15 0:06 
Questionyou didnt explain the webconfig setting,however it doesnt work Pin
varun15019-Oct-15 18:30
Membervarun15019-Oct-15 18:30 
Questionunable to generate a meta address Pin
Member 1179016024-Jun-15 10:53
MemberMember 1179016024-Jun-15 10:53 
AnswerThanks Pin
Member 1168461022-Jun-15 23:32
MemberMember 1168461022-Jun-15 23:32 
QuestionThanks Pin
Member 1161579617-Apr-15 4:43
MemberMember 1161579617-Apr-15 4:43 
QuestionIt doesn't work for me Pin
Nadeesha Gayan Premadasa18-Feb-15 18:57
MemberNadeesha Gayan Premadasa18-Feb-15 18:57 
GeneralVote of 5 in response of "Vote of 1's" Pin
Kobus du Toit9-Feb-15 20:36
MemberKobus du Toit9-Feb-15 20:36 
GeneralMy vote of 5 Pin
karenpayne9-Sep-14 7:26
Memberkarenpayne9-Sep-14 7:26 
QuestionUnable to connect Pin
Riya200327-Mar-14 5:55
MemberRiya200327-Mar-14 5:55 
Questionconsultation Pin
jhonatan02995-Dec-13 12:39
Memberjhonatan02995-Dec-13 12:39 
AnswerRe: consultation Pin
sgissinger13-Nov-14 2:04
Membersgissinger13-Nov-14 2:04 
GeneralMy vote of 1 Pin
baytu2-Oct-13 0:07
Memberbaytu2-Oct-13 0:07 
GeneralRe: My vote of 1 Pin
sgissinger13-Nov-14 2:06
Membersgissinger13-Nov-14 2:06 
GeneralMy vote of 1 Pin
baytu2-Oct-13 0:06
Memberbaytu2-Oct-13 0:06 
GeneralMy vote of 5 Pin
cstogian31-Aug-13 2:17
Membercstogian31-Aug-13 2:17 
QuestionHow about POST a json string to the request? Pin
franklinraj11-Jul-13 6:30
Memberfranklinraj11-Jul-13 6:30 
SuggestionAn alternative walkthough.. Pin
Michael Gledhill5-Feb-13 8:06
MemberMichael Gledhill5-Feb-13 8:06 
GeneralRe: An alternative walkthough.. Pin
karenpayne3-Mar-14 9:31
Memberkarenpayne3-Mar-14 9:31 
GeneralRe: An alternative walkthough.. Pin
Fawad Raza21-Dec-15 0:03
MemberFawad Raza21-Dec-15 0:03 
GeneralMy vote of 5 Pin
Joel Ivory Johnson30-Jan-13 5:56
mvaJoel Ivory Johnson30-Jan-13 5:56 
QuestionPublish it on IIS 7 Pin
m.gaber26-Dec-12 4:26
Memberm.gaber26-Dec-12 4:26 

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.