Introduction to RESTFulService
RESTful services are those which follow Representational State Transfer architecture style. WCF allows us to make calls and exchange messages using SOAP over a variety of protocols i.e. HTTP, TCP, Named Pipes and MSMQ etc. In a scenario, if we are using SOAP over HTTP, we are just utilizing HTTP as a transport. But HTTP is much more than just a transport. So, when we talk about REST architectural style, it dictates that “Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, simply HTTP should be used for making calls”.
RESTful architecture uses HTTP for all CRUD operations like (Read/Create/Update/Delete) using simple HTTP verbs like (GET, POST, PUT, and DELETE).It’s simple as well as lightweight. For the sake of simplicity, I am going to implement only a GET request for which service will return certain types of data (i.e. Product data) in XML format.
Following are 5 simple steps to create your first RESTful service that returns data in XML.
- Create a WCF Service Project.
- Preparing the data (e.g. Product) to return
- Creating Service Contract
- Implementing Service
- Configure Service and Behavior
Now lets create the service.
- Open Visual Studio.
- File - New Project - new WCF Service Application.
- Now we will create entity which needs to be share. Add Employee.cs
- [DataContract]
- public class Employee {
- [DataMember]
- public int EmpId {
- get;
- set;
- }
- [DataMember]
- public string Name {
- get;
- set;
- }
- [DataMember]
- public string DepartmentName {
- get;
- set;
- }
- [DataMember]
- public int Age {
- get;
- set;
- }
- }
Now add one more class which will return the collection of employees
- public partial class Employees {
-
-
- public static readonly Employees _emps = new Employees();
- public static Employees Instance {
- get {
- return _emps;
- }
- }
- public List < Employee > emps {
- get {
- return empList;
- }
- }
- private List < Employee > empList = new List < Employee > () {
- new Employee() {
- Name = "e1", DepartmentName = "D1", Age = 25
- },
- new Employee() {
- Name = "e2", DepartmentName = "D2", Age = 35
- },
- new Employee() {
- Name = "e3", DepartmentName = "D3", Age = 45
- },
- new Employee() {
- Name = "e4", DepartmentName = "D4", Age = 26
- },
- };
- }
Now Add Service i.e. EmployeeRESTService.svc [ServiceContract]
- public interface IEmployeeRESTService {
- [OperationContract]
- void DoWork();
- [OperationContract]
- [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetEmployeeLsit/")]
- List < Employee > GetEmployeeLsit();
- }
- public class EmployeeRESTService: IEmployeeRESTService {
- void IEmployeeRESTService.DoWork() {
- throw new NotImplementedException();
- }
- List < Employee > IEmployeeRESTService.GetEmployeeLsit() {
- return Employees.Instance.emps;
- }
- }
IEmployeeRESTService contains only one method i.e. GetEmployeeLsit. Important points to understand about this method are WebInvoke attribute parameters.
- Method = “GET”, represents an HTTP GET request.
- ResponseFormat = WebMessageFormat.Xml, response format will be XML here but we can return JSON as well by changing its value to WebMessageFormat.json.
- BodyStyle = WebMessageBodyStyle.Bare, indicates neither the request and nor response are wrapped. Other possible values for BodyStyle are Wrapped, WrappedRequest, WrappedResponse.
- UriTemplate = “GetEmployeeLsit /”, it has two parts, URL path and query.
Now the last step is to configure Service and Behavior
- <services>
- <service name="WcfService1.EmployeeRESTService" behaviorConfiguration="serviceBehaviors">
- <endpoint address="" binding="webHttpBinding" contract="WcfService1.IEmployeeRESTService" behaviorConfiguration="web"> </endpoint>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="serviceBehavior">
- <serviceMetadata httpGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="false" /> </behavior>
- </serviceBehaviors>
- <endpointBehaviors>
- <behavior name="web">
- <webHttp/> </behavior>
- </endpointBehaviors>
- </behaviors>
- <serviceHostingEnvironment multipleSiteBindingsEnabled=”true” /> </system.serviceModel>
webHTTPBinding is the binding used for RESTful services.
Now, everything about creating RESTful service is done. You can easily run and test it.