Thursday, 26 December 2013

Create a WCF Rest service with response type xml and json

In article we are going to see how to create a WCF rest service with response type as xml and json. To create REST service three things need to be kept in mind.

1. Endpoint Binding should be webHttpBinding
2. Create  EndPoint behaviour and add <webHttp />
3. Create a UriTemplate and specify the WebGet or WebInvoke attribute

UriTemplate : Specify what kind of uri need to specify to that method
ResponseFormat : Specify the Response Format Xml or Json
Method :   POST or GET

web.config

    <behaviors>
      <serviceBehaviors>
        <behavior name="Restsample.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="Rest">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>


<endpoint address="" behaviorConfiguration="Rest" binding="webHttpBinding" contract="Restsample.IService1">  

C# code

  public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate="/",ResponseFormat=WebMessageFormat.Json)]
        int Add();

        [OperationContract]
        [WebGet(UriTemplate="/GetEmp",ResponseFormat=WebMessageFormat.Xml)]
        List<Employee> GetEmployees();

        [OperationContract]
        [WebInvoke(UriTemplate="/Add?a={a}",Method="GET",ResponseFormat=WebMessageFormat.Json)]
        int AddOperation(int a);

        [OperationContract]
        [WebInvoke(UriTemplate = "/Sub?a={a}&b={b}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        int SubOperation(int a,int b);

    }
}

public class Service1 : IService1
    {
            
        public int Add()
        {
            return 10;
        }


        public List<Employee> GetEmployees()
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee() { EmpID = 1,EmpName="Rajesh",Address="Chennai"});
            employees.Add(new Employee() { EmpID = 2, EmpName = "Suresh", Address = "Mumbai" });
            employees.Add(new Employee() { EmpID = 3, EmpName = "Ramesh", Address = "Bangalore" });
            employees.Add(new Employee() { EmpID = 4, EmpName = "Siva", Address = "Delhi" });
            employees.Add(new Employee() { EmpID = 5, EmpName = "Farath", Address = "Hyderabad" });
            return employees;
        }


        public int AddOperation(int a)
        {
            return 1 + a;
        }


        public int SubOperation(int a, int b)
        {
            return a + b;
        }
    }


    [DataContract]
    public class Employee
    {
        [DataMember(Order=1)]
        public int EmpID { set; get; }

        [DataMember(Order=2)]
        public string EmpName { set; get; }

        [DataMember(Order=3)]
        public string Address { set; get; }
    }


Invoke service from browser :

Call a AddOperation method in browser 

output : 2


Following uri will call the Add method
http://localhost/Restsample/service1.svc/

output: 10

Following Uri will call the add employee method
http://localhost/Restsample/service1.svc/GetEmp

output:

Json

[
    {"EmpID":1,"EmpName":"Rajesh","Address":"Chennai"},
    {"EmpID":2,"EmpName":"Suresh","Address":"Mumbai"},
    {"EmpID":3,"EmpName":"Ramesh","Address":"Bangalore"},
    {"EmpID":4,"EmpName":"Siva","Address":"Delhi"},
    {"EmpID":5,"EmpName":"Farath","Address":"Hyderabad"}
]

Xml

<ArrayOfEmployee xmlns="http://schemas.datacontract.org/2004/07/Restsample" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Employee>
        <EmpID>1</EmpID>
        <EmpName>Rajesh</EmpName>
        <Address>Chennai</Address>
    </Employee>
    <Employee>
        <EmpID>2</EmpID>
        <EmpName>Suresh</EmpName>
        <Address>Mumbai</Address>
    </Employee>
    <Employee>
        <EmpID>3</EmpID>
        <EmpName>Ramesh</EmpName>
        <Address>Bangalore</Address>
    </Employee>
    <Employee>
        <EmpID>4</EmpID>
        <EmpName>Siva</EmpName>
        <Address>Delhi</Address>
    </Employee>
    <Employee>
        <EmpID>5</EmpID>
        <EmpName>Farath</EmpName>
        <Address>Hyderabad</Address>
    </Employee>
</ArrayOfEmployee>



From this article we can understand how to create a REST WCF service.

No comments:

Post a Comment