The initial steps in designing RESTful based services is to identify the objects (resources) that will be exposed to the outside world and next to map these resources to a URI. We should not focus on designing the methods for an application; instead, we should focus on designing resources and their URI. There are various data formats which can be used with REST, but XML is easier and mostly used, but JSON is equally being used.
.NET 4 and REST and WCF
WCF is not all about building SOAP based services, it's an extensible framework with a common programming model and a totally pluggable communication infrastructure. The basic job of the WCF runtime is to listen for messages from a network location and process those messages and pass them to the application (service). With .NET 4, developing a REST application is an easy task. Microsoft has provided a REST template which we can use to create new projects, this will create a basic skeleton code for REST.
From VS 2010, create a new project, select an online template, and WCF. There are various options displayed, select "WCF REST Service Template 40".
Create a class which returns a collection of employee details. Mark the method which we want to call with the attribute
[WebGet()]
.[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// NOTE: If the service is renamed, remember to update the global.asax.cs file
public class Service1
{
// TODO: Implement the collection resource
// that will contain the SampleItem instances
[WebGet(UriTemplate = "")]
public IList<EmployeeDetail> GetEmployeeDetail()
{
IList<EmployeeDetail> employeeDetail =
new List<EmployeeDetail>();
EmployeeDetail empDetail1 = new EmployeeDetail();
empDetail1.EmployeeIdentifier = 123;
empDetail1.EmployeeName = "Test1";
empDetail1.ProjectName = "NE";
employeeDetail.Add(empDetail1);
EmployeeDetail empDetail2 = new EmployeeDetail();
empDetail2.EmployeeIdentifier = 1234;
empDetail2.EmployeeName = "Test2";
empDetail2.ProjectName = "NE";
employeeDetail.Add(empDetail2);
return employeeDetail;
}
}
public class EmployeeDetail
{
private string employeeName;
private int employeeId;
private string projectName;
public int EmployeeIdentifier
{
get
{
return employeeId;
}
set
{
employeeId = value;
}
}
public string EmployeeName
{
get
{
return employeeName;
}
set
{
employeeName = value;
}
}
public string ProjectName
{
get
{
return projectName;
}
set
{
projectName = value;
}
}
}
Build and run the code. Now access the URL http://localhost:8422/Service1/. The following XML is returned from the service:
<ArrayOfEmployeeDetail xmlns="http://schemas.datacontract.org/2004/07/WcfRestService2"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<EmployeeDetail>
<EmployeeIdentifier>123</EmployeeIdentifier>
<EmployeeName>Test1</EmployeeName>
<ProjectName>NE</ProjectName>
</EmployeeDetail>
<EmployeeDetail>
<EmployeeIdentifier>1234</EmployeeIdentifier>
<EmployeeName>Test2</EmployeeName>
<ProjectName>NE</ProjectName>
</EmployeeDetail>
</ArrayOfEmployeeDetail>
Source collected from Code Project
No comments :
Post a Comment