Click here to Skip to main content
14,326,074 members

Building and Testing WCF RESTful services

Rate this:
4.71 (9 votes)
Please Sign up or sign in to vote.
4.71 (9 votes)
12 Mar 2012CPOL
Building and Testing WCF RESTful services


Following article will cover, how to build a RESTful service using the WCF framework. What I also want to achieve is to have multiple end points.

1. RESTful end point

2. TCP IP end point.

By doing this, we can try to cater to different set of clients. RESTful services can be accessed by all kind of clients and TCP IP can be access by .Net clients. As many of you would already aware that the TCP/IP is the best suited for better performance. At the end of article, I will also cover how you can test your REST based service using a fiddler tool.

What is REST

• REST stands for Representational State Transfer

• REST is about accessing resources through a universal interface.

• REST URI: Uniform Resource Identifier - This is a unique way of identifying resources on the network.

• You can also refer following link to get to know all REST terminologies.

Steps used to Achieve the above stated Objective

Add Services

• Add a library project to a solution

• Implement services using IService interface and Service Contract.

• Service will in turn interact with Northwind database.

Host Services

• Add a console application and

• Configure the app.config with TCP/IP and RESFful service.

• Host services in the console application.

Test Services

• Create a windows application to consume services.

1. RESTful endpoint

2. TCP/IP endpoint

• Test Restful Services using Browser and Fiddler tool

Add Services

• Create a .Net solution

• Add a library project with name SampleServices.

• Add “LINQ to SQL” class, Northwind.dbml.

I want to communicate to category table of Northwind database. Once you have Added Northwind.dbml file, connect to a local server using server explorer in the LINQ designer. Then drag and drop category table on to the LINQ designer.


• You will have to change the SQL connection string to point to your local server, which has northwind database.

• Add c# classes IService and Service

• Make sure you add following reference System.ServiceModel & System.ServiceModel.Web

• IService Interface Code

    public interface IService
        [WebInvoke(UriTemplate = "/Category/{categoryID}", Method = "GET")]
        Category GetCategory(string CategoryID);
        [WebInvoke(UriTemplate = "/Category", Method = "GET")]
<category /> GetCategories();
        [WebInvoke(UriTemplate = "/Category/Add", Method = "POST")]
        bool AddCategory(Category category);

• Service Code

    public class Service : IService
        public Category GetCategory(string categoryID)
            NorthwindDataContext context = new NorthwindDataContext();
            var category = context.Categories.SingleOrDefault(e => e.CategoryID == Convert.ToInt32(categoryID));
            return category;
        public List
<category /> GetCategories()
            NorthwindDataContext context = new NorthwindDataContext();
            var categories = context.Categories.ToList();
            return categories;
        public bool AddCategory(Category category)
            NorthwindDataContext context = new NorthwindDataContext();
            return true;

Host Services

• Add a console application “ServiceHost”

• Make sure you add reference to System.ServiceModel assembly.

• Add Reference to SampleService.dll

• Configure the app.config to support REST and TCP/IP endpoints

    <service name="SampleServices.Service" behaviorConfiguration="MYServiceBehavior">
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      <endpoint address="net.tcp://localhost:12000/SampleServices/Service" binding="netTcpBinding" bindingConfiguration="TCPBindingDetails" contract="SampleServices.IService" behaviorConfiguration="TCPBehavior">
      <endpoint name="webHttpBinding" address="REST" binding="webHttpBinding" behaviorConfiguration="RESTBehavior" contract="SampleServices.IService" />
          <add baseAddress="<a href="http://localhost:9002/SampleServices/Service/">http://localhost:9002/SampleServices/Service/</a />" />
        <timeouts closeTimeout="01:20:10" openTimeout="01:20:00" />
      <binding name="TCPBindingDetails" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="5000000" maxBufferSize="5000000" maxConnections="10" maxReceivedMessageSize="5000000">
        <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        <reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="false" />
        <security mode="Transport">
          <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          <message clientCredentialType="None" />
      <clear />
      <behavior name="TCPBehavior">
        <dataContractSerializer maxItemsInObjectGraph="6553600" />
      <behavior name="RESTBehavior">
        <dataContractSerializer maxItemsInObjectGraph="6553600" />
        <webHttp helpEnabled="true" />
      <behavior name="MYServiceBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="True" />
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

Note: helpEnabled="true"<webhttp helpenabled="true">, is used to enable Automatic help pages for the RESTful service

• Hosting code in program.cs

static void Main(string[] args)
    // Create a ServiceHost for the CaseStudyService type.
    System.ServiceModel.ServiceHost serviceHost = new

    // Open the ServiceHost to create listeners and start listening for messages.
    Console.WriteLine("Services are ready & running.");

Test Services

• Create a windows application, “Client”

• Add references to System.ServiceModel & System.Runtime.Serialization

• Generate proxy using svcutil.exe

• Add proxy class to the client application.


• Sample code to access TCP IP end point.

ServiceClient client = new ServiceClient();
var allCategories = client.GetCategories();

• Sample Code to access REST service.

WebRequest request = WebRequest.Create("http://localhost:9002/SampleServices/Service/REST/Category/1");
WebResponse ws = request.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(ws.GetResponseStream());
string response = responseStream.ReadToEnd();

• You can also test the REST based services through Browser and Fiddler tool.

• Important TIP: You can get to know following items by enabling help on the service


o Method type

o Description

o Data format expected for GET method

o Data format or Request Body for POST method.

You can get all these information by browsing to http://localhost:9002/SampleServices/Service/REST/help


Clicking on the method will provide the sample response XML


Clicking on the POST method will provide the Request xml to be sent.


Get all categories in browser using RESTful service http://localhost:9002/SampleServices/Service/REST/Category


• Get the details of the category whose ID = 1, http://localhost:9002/SampleServices/Service/REST/Category/1


• Using Fiddler: Go to request builder in the fiddler tool and type in the url, select GET method. Then click execute button. url: http://localhost:9002/SampleServices/Service/REST/Category/

Note: Make sure that you are running the WCF host


• You can see the response by double clicking on the request in web session panel.


• You can see the use the fiddler for POST method also. In order add a category, we need to use URI http://localhost:9002/SampleServices/Service/REST/Category/Add , which is a POST method. We can get the request xml by using the help url.

2147483647 String content String content 1999-05-31T11:20:00 QmFzZSA2NCBTdHJlYW0=

Let us use the above format in the Fiddler tool add a new category. Remove CategoryID, as this is auto generated column in the database. Use the below format in the Fiddler tool

New Category New Category 2012-05-31T11:20:00

• Select “POST” method in the method. Provide url


Add content-type: application/xml in the request header.

Add the above XML in the request body.


• Click Execute and check in DB, if the category is inserted. You can also check it by using RESTfule service in browser. http://localhost:9002/SampleServices/Service/REST/Category




Article covers having multiple endpoints for the WCF service. Service was configured with TCP/IP and RESTful endpoint. This might be useful in those projects, where you would want your services to be consumed by both .Net and other varied clients. .Net application can still communicate with TCP/IP endpoint, which provides the optimal performance; where as other clients can communicate with RESTful end point. I have also tried to cover different ways to test the services. We can also use WCF REST started kit to create service. I am yet to use it, and will elaborate it once I start using it.


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


About the Author

Manjunath Shrikantiah
India India
9+ plus years of experience in IT industry. This includes experience in architecting, designing and developing solutions on Web and desktop application platforms

Comments and Discussions

PraiseLove it! great examples my vote 5+ Pin
Member 119560656-Dec-17 4:31
memberMember 119560656-Dec-17 4:31 
QuestionRest Service need to be host in windows service Pin
RamanIndia12-Sep-13 19:57
memberRamanIndia12-Sep-13 19:57 
GeneralMy vote of 5 Pin
vignesh chennai6-Jun-13 2:58
membervignesh chennai6-Jun-13 2:58 
Questionmy mark is 5 Pin
vignesh chennai6-Jun-13 2:57
membervignesh chennai6-Jun-13 2:57 
QuestionIs it really a Restful? Pin
Alexsandro_xpt13-Feb-13 3:25
memberAlexsandro_xpt13-Feb-13 3:25 
QuestiondataContractSerializer Pin
Dan_Barack6-Nov-12 20:11
memberDan_Barack6-Nov-12 20:11 
GeneralMy vote of 5 Pin
Vijay Kanda18-Oct-12 21:49
memberVijay Kanda18-Oct-12 21:49 
GeneralRe: My vote of 5 Pin
Vijay Kanda18-Oct-12 21:52
memberVijay Kanda18-Oct-12 21:52 
GeneralExcellent Article Pin
osama zain2-Oct-12 23:41
memberosama zain2-Oct-12 23:41 
GeneralRe: Excellent Article Pin
Manjunath Shrikantiah3-Oct-12 18:18
memberManjunath Shrikantiah3-Oct-12 18:18 
Questionpls prvide me perform All operations using DATABASE and Consume This service in my ASP.NET webpages Pin
Member 917112826-Jul-12 2:00
memberMember 917112826-Jul-12 2:00 
QuestionHow to consume WCFREST service in my ASP.NET website Pin
Member 917112823-Jul-12 16:29
memberMember 917112823-Jul-12 16:29 
AnswerRe: How to consume WCFREST service in my ASP.NET website Pin
Manjunath Shrikantiah23-Jul-12 18:00
memberManjunath Shrikantiah23-Jul-12 18:00 
QuestionHow to perform CURD operations on WCFREST service&also consume this service in ASP.NET Webpage perform CRUD operationd Pin
Member 91711284-Jul-12 0:44
memberMember 91711284-Jul-12 0:44 
Questioncall .net WCF in java with post request content Pin
samSinghIndia15-May-12 1:13
membersamSinghIndia15-May-12 1:13 
SuggestionRe: call .net WCF in java with post request content Pin
vyas_pratik204-Jun-12 2:57
groupvyas_pratik204-Jun-12 2:57 
Questiongreat Pin
goran pesov26-Apr-12 3:40
membergoran pesov26-Apr-12 3:40 
AnswerRe: great Pin
Manjunath Shrikantiah26-Apr-12 18:53
memberManjunath Shrikantiah26-Apr-12 18:53 

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.

Posted 12 Mar 2012

Tagged as


62 bookmarked