Introduction
We can use WCF to build Restful services in .NET.
RESTful Service
Representation State Transfer (REST) is an architecture style. It is a set of constraints based on the architectural style of the World Wide Web. Concepts to building RESTful services using Windows Communication Foundation (WCF). REST embraces the Web and HTTP. REST services are built to follow the specific constraints of the REST architectural style.
RESTful services model the interaction with user agents based on resources. Each resource is represented by a unique URI and the user agent uses the uniform interface of HTTP to interact with a resource via that URI. In REST, resources are identified by a unique URI. This is one of the constraints of architectural style. The following figure shows the four main verbs of the uniform interfaces GET, POST, PUT and DELETE.
Implementing Simple RESTful Service Example
Create your first simple RESTful service that returns data in XML format. The following is the procedure for creating the RESTful service.
- Open Visual Studio.
- Select "File" -> "New" -> "Project...", select WCF from the left and create a new WCF Service Application. Name it “SimpleRESTfulService”.
- Now add a class to the project named Employee. We use this class to return the data. Add the following code to that class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Web;
-
- namespace SimpleRESTfulService
- {
- [DataContract]
- public class Employee
- {
- [DataMember]
- public int EmpID { get; set; }
-
- [DataMember]
- public string EmpName { get; set; }
-
- [DataMember]
- public string City { get; set; }
-
- [DataMember]
- public string EmpDesg { get; set; }
- }
- }
- Add one method to the IService1 interface.
GetEmpList() like the following.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
-
- namespace SimpleRESTfulService
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Xml,
- BodyStyle = WebMessageBodyStyle.Bare,
- UriTemplate = "GetEmpList/")]
- List<Employee> GetEmpList();
- }
- }
Now understand the following methods WebInvoke attribute parameters:
- Method = “GET”, represents an HTTP GET request.
- ResponseFormat = WebMessageFormat.Xml, response format will be XML here but we can return JSON also.
- BodyStyle = WebMessageBodyStyle.Wrapped, indicates both the request and response are wrapped.
- UriTemplate = “GetEmpList/”, it has two parts, URL path and query.
- Now implement a RESTful service and add the GetEmpList() method to the Service1 class like the following.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
-
- namespace SimpleRESTfulService
- {
- public class Service1 : IService1
- {
-
- List<Employee> lstEmployee = new List<Employee>()
- {
- new Employee{ EmpID = 1, EmpName = "Krishna", City ="Mathura", EmpDesg = "Project Manager"},
- new Employee{ EmpID = 2, EmpName = "Radha", City ="Mathura", EmpDesg = "Practice Manager"},
- new Employee{ EmpID = 3, EmpName = "Jeetendra", City ="Pune", EmpDesg = "Software Developer"},
- new Employee{ EmpID = 4, EmpName = "Anil", City ="Pune", EmpDesg = "Administrator"},
- };
-
-
-
-
-
- public List<Employee> GetEmpList()
- {
- try
- {
- return lstEmployee;
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
- Configure the service and behaviors in the Web.config file like the following:
- <?xml version="1.0"?>
- <configuration>
- <appSettings>
- <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
- </appSettings>
- <system.web>
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5"/>
- </system.web>
- <system.serviceModel>
- <services>
- <service name="SimpleRESTfulService.Service1" behaviorConfiguration="serviceBehavior">
- <endpoint address="" binding="webHttpBinding" contract="SimpleRESTfulService.IService1" behaviorConfiguration="web"></endpoint>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="serviceBehavior">
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- </serviceBehaviors>
- <endpointBehaviors>
- <behavior name="web">
- <webHttp/>
- </behavior>
- </endpointBehaviors>
- </behaviors>
- <protocolMapping>
- <add binding="basicHttpsBinding" scheme="https" />
- </protocolMapping>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
- </system.serviceModel>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- <directoryBrowse enabled="true"/>
- </system.webServer>
- </configuration>
In this config file we added the Service and Behaviors, this webHttpBinding is the binding used for the RESTful service.
- Right-click on the Service1.svc.cs file and it will show like the following:
Just modify the URL in the browser and add “GetEmpList/” to it. So, this is the UriTemplete we defined as the service contract method. It will show the record of employees in XML format.
Summary
I hope you understand the concept of creating WCF RESTful services. If you have any suggestion regarding this article then please contact me.