TechBubbles Microsoft Technology BLOG

REST Based WCF Service in ASP.NET 3.5


This post explains you about building a REST-based service using Windows Communication Foundation(WCF) and ASP.NET 3.5. We develop this service without using the SOAP.You can read the REST Overview to get an understanding of what REST is? In simple tetchier statement we can say REST is WCF Web programming model.

1. Create a new web site in VS 2008 as follows


Select the HTTP from the location box and type the AdventureWeb in the name location box.

2. Add a new item by right clicking on the web site and select the LINQ to SQL Classes item from the dialogue box.


3.Drag the Employee table from the server explorer to the DBdesigner as follows


4.Add the EmployeeService.cs file to the App_Code folder and add the System.ServiceModel assembly to the web site. Write the following using statements in the EmployeeService Class.

   1: using System.Web;
   2: using System.ServiceModel;
   3: using System.Runtime.Serialization;
   4: using System.Collections.Generic;
   5: using System.ServiceModel.Web;
   6: using System.ServiceModel.Activation;

5.Add the ServiceContract and AspNetCompatibilityRequirementsMode attributes to the above created class

   1: [ServiceContract()]
   2: [AspNetCompatibilityRequirements(
   3: RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   4: public class EmployeeService
   5: {
   6:     public EmployeeService()
   7:     {
   9:     }
  10: }

6.Add the new class EmployeeResult.cs file to the web site then Add the DataContract attribute and two public fields to this class as follows.


   1: using System.Runtime.Serialization;
   3: [DataContract(Namespace = "", Name = "Employee")]
   4: public class EmployeeResults
   5: {
   6:     [DataMember]
   7:     public string EmployeeName;
   9:     [DataMember]
  10:     public string Link;
  11: }

7.Add the following code to the EmployeeService class

   1: [OperationContract]
   2: [WebGet(UriTemplate = "/")]
   3: public EmployeeResults[] GetAllEmployee()
   4: {
   5:     string strConnection = ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString;
   6:     DataClassesDataContext dc = new DataClassesDataContext(strConnection);
   7:     string strUrl = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString();
   8:     var result = from anEmp in dc.employee
   9:                  select new EmployeeResults
  10:                  {
  11:                    EmployeeName = dc.emp_name;
  12:                    Link = string.Format("{0}{1}",currentURL,dc.emp_name);
  13:                  }
  14:     return result.ToArray();
  16: }

8.Add the new item to the web site by right clicking on solution explorer select the text file from the dialogue box then name it EmpService.svc.

9. Add the service host declaration with the following attributes


10. Now you have the REST service that returns XML and it can be consumed by any REST client. It would not be a browser.

11. See the svc file in browser which returns the employee names in xml format.


We have developed REST based WCF service in ASP.NET 3.5 without using the SOAP and can be consumed by any client. We can also use this service in ASP.NET Web sites.

Share this post :

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.


  • Thanks for the REST WCF article and sample. Just one minor coment:
    It would be great if you can post code without the line numbers. That would allow novices/beginners to copy and set up sample much easier and faster.

    Keep up the good work. I enjoy the interesting articles from this web site. Thanks

  • Could not get this sample to compile as follows:

    select new EmployeeResults
    EmployeeName = dc.emp_name // emp_name not in dc
    Link = string.Format(“{0}{1}”, strUrl, dc.emp_name);
    Both statements have the same error emp_name not defined in dc Everything else seemed to work up to this point. Any suggestions about this problem? Thanks.

  • thnx good example but I have downloaded REST starter kit preview 2 can u tell me what this starter kit with new Class HttpClient???

  • Instead of below code:
    select new EmployeeResults
    EmployeeName = dc.emp_name // emp_name not in dc
    Link = string.Format(”{0}{1}”, strUrl, dc.emp_name);

    It should be as per below code:
    var Result = from anEmp in dc.Employees
    select new EmployeeResult
    EmployeeName = anEmp.Name,
    Link = string.Format(“{0}{1}”,strUrl,anEmp. Name)

  • To the guy who does not like line numbers:
    You can remove them by holding ALT while selecting text in Visual Studio.
    This will give you a rubber band type of selection style.

  • I am getting this error.

    A name was started with an invalid character. Error processing resource ‘http://localhost/AdventureWeb/EmpService.svc’. Li…

    <%@ServiceHost Language="C#" Service="EmpService"


  • i just removed all this code and


    public EmployeeResults[] GetAllEmployee()


    string strConnection = ConfigurationManager.ConnectionStrings[“AdventureWorksConnectionString”].ConnectionString;

    DataClassesDataContext dc = new DataClassesDataContext(strConnection);

    string strUrl = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString();

    var result = from anEmp in dc.employee

    select new EmployeeResults

    EmployeeName = dc.emp_name;

    Link = string.Format(“{0}{1}”,currentURL,dc.emp_name);


    return result.ToArray();


    replce with


    public DataTable GetAllEmployee()


    DataTable dtProduct = new DataTable();

    MySqlDataAdapter daState = new MySqlDataAdapter();

    const string ConStr = @”Server=;Database=sampath;User=root;Password=root;Convert Zero Datetime=true;Allow Zero Datetime=True;CharSet=utf8;Connect Timeout=10;port=3306″;

    MySqlConnection conn = new MySqlConnection(ConStr);

    MySqlCommand cmd = new MySqlCommand();

    DataSet dsProducts = new DataSet();

    cmd.Connection = conn;

    string strSortOrder = string.Empty;

    string strBycategory = string.Empty;

    cmd.CommandText = @”select Name from sampath”;

    daState = new MySqlDataAdapter(cmd);

    daState.Fill(dsProducts, “Products”);

    dtProduct = dsProducts.Tables[0];


    return dtProduct;

    is it k

TechBubbles Microsoft Technology BLOG

Follow me


Tag Cloud