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 :

%d bloggers like this: