C # implement rest-style Web Services
Rest (representational state transfer) is a term proposed by royfielding to describe the architecture style of the interconnected system. Why is it called rest? The Web is essentially composed of various resources, which are uniquely identified by Uris. A browser (or any other application similar to a browser) shows a way or status of the resource. If a user directs a link to another resource on this page, the user accesses the resource and shows its status. This means that the client application changes the status with the performance of each resource, that is, rest.
A typical soap-based Web Service is centered on operations. Each operation accepts XML documents as input and provides XML documents as output. In essence, they are RPC-style. In Roa applications that follow the rest principle, services are resource-centric and operations on each resource are standard HTTP methods.
Let's take a look at how to use C # To implement a rest-style Web Service:
· Define the service contract
· Define URL Routing
· Service implementation
· Compile a Host Program for the service
Define the service contract
Use vs2010 to create a new class library project named restservicelib and add references to the project system. servicemodel and system. servicemodel. Web. Create a CS file named irestdemoservices. CS and define the following interface:
public interface IRESTDemoServices{ stringGetClientNameById(string Id);}
To enable. netframework to identify this as a service interface, we add the servicecontract feature to the interface and the operationcontract feature to the method:
[ServiceContract(Name = "RESTDemoServices")]public interface IRESTDemoServices{ [OperationContract] string GetClientNameById(int Id);}
Of course, we can define more methods in this service interface. Here is a simple example without adding other methods.
Define URL Routing
Create a routing class
public static class Routing{ public const string GetClientRoute = "/Client/{id}";}
Associate routing with methods in the service interface. Note that the parameter IDs in routing and service must be consistent (Case Insensitive)
[ServiceContract(Name = "RESTDemoServices")]public interface IRESTDemoServices{ [OperationContract] [WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)] string GetClientNameById(string Id);}
The webget feature specifies that the getclientnamebyid can be accessed by a specific URL using the get method.
Implement Service
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class RestDemoServices:IRESTDemoServices{ public string GetClientNameById(string Id) { Random r = new Random(); string ReturnString=""; int Idnum=Convert.ToInt32(id); for (int i = 0; i < Idnum; i++) ReturnString += char.ConvertFromUtf32(r.Next(65, 85)); return ReturnString; }}
The implementation of this method (getclientnamebyid) is only to return a random string with the length of ID based on the ID. In actual projects, it is more likely to access the database to obtain the desired information and then return it.
Compile a Host Program for the service
Use vs2010 to create a new console application project. In the project properties, set targetframework. NET framework 4. The default value is. net framework4 client profile, because. net framework4 client profile generally does not support server-side helper classes. Add reference system. servicemodel. web for the project, and add the restservicelib project as reference. The code implementation is as follows:
static void Main(string[] args){ RestDemoServices DemoServices = new RestDemoServices(); WebServiceHost _serviceHost = new WebServiceHost(DemoServices, new Uri("http://localhost:8000/DEMOService")); _serviceHost.Open(); Console.ReadKey(); _serviceHost.Close();}
The above Code provides the host environment for restdemoservices and specifies its address as http: // localhost: 8000/demoservice. Run this program and open IE and enter http: // localhost: 8000/demoservice/client/22
To use IIS as the host, create a new SVC file with the following content:
<%@ ServiceHost Language="C#" Debug="true" Service="RESTService.Lib.RestDemoServices" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>