What Are RESTful Web Services?
REST stands for Representational State Transfer is an architectural style rather than a prescribed way of building Web services, some of the most important aspects of the REST environment are:
- HTTP or HTTPS may be used as the transfer protocol.
- URLs including query strings are used to address resources.
- Representation formats supported range from HTML and XML to JSON and ATOM.
- A Simple and intuitive programming interface is achieved by using HTTP verbs and status codes.
- Statelessness in the interaction between clients and services.
REST is not concerned with the definition of messages and the design of methods, the key point here is that REST describes a stateless, hierarchical scheme for representing resources and business objects over a network.
The main components of this model are: resources and actions.
The action of the resource is determined by four main HTTP verbs: GET, PUT, DELETE and POST, and the action which can affect those resources are mainly CRUD (Create, Read, Update and Delete) methods, the success of the action is found by the HTTP status code.
The REST model relies on the application that accesses the data sending the appropriate HTTP verb as part of the request used to access the data.
- GET is used exclusively to retrieve data and, therefore, the result can also be buffered.
- POST is used to add new records.
- PUT is used to add or change a resource.
- DELETE is used for delete resources.
The data can be returned in a number of formats, but for portability the most common formats include XML (POX) and JSON.
WCF and REST
The REST architecture is becoming increasingly common, and WCF provides attributes, methods, and types with which you can build and access REST Web Services quickly and easily.
- WebHttpBinding: An binding that uses the HTTP transport and text message encoder.
- WebBehavior: This is an endpoint behavior that will modify the dispatch layer on all operations on a contract. The modifications cause messages to be dispatched to methods on your service based on URIs and HTTP verbs.
- WebServiceHost: This is a ServiceHost-derived class that simplifies the configuration of a web-based service.
- WebOperationContext: This is a new context object, which contains the state of the incoming request and ongoing response, and simplifies coding against HTTP using WCF.
- WebGetAttribute/WebInvokeAttribute: Operation behaviors that are applied as attributes on a ServiceContract’s methods. WebGetAttribute is for GET verb and WebInvokeAttribute is for all the other verbs. It also tells the dispatcher how to match the methods to URIs and how to parse the URI into method parameters.
The following table shows the properties of both WebGetAttribute and WebInvokeAttribute.
|Method||The HTTP verb the method should respond to.|
|UriTemplate||The definition of the URI the CLR method should respond to.|
|RequestFormat||Enumeration that specifies the format for deserializing the request (Xml or Json).|
|ResponseFormat||Enumeration that specifies the format for serializing the response (Xml or Json).|
|BodyStyle||Enumeration that specifies whether the request and the response data should be wrapped in an element with the same name as the CLR method name. Bare is typically used with RESTful services.|
The essential components to construct a REST Service with WFC can be found in System.ServiceModel.Web assembly.
However, the most important part of the process is designing the schema that you will use to provide access to the resources exposed by the service.
So the main idea behind REST is to design your URIs in a way that makes logical sense based on your resource set. The URIs should, if possible, make sense to the application that consumes the data.
Depending on the volume of data in the database, a query might retrieve a large number of items, therefore, it makes sense to provide additional query parameters that a user can specify to limit the number of items returned.
Implementing a simple RESTful Service Example with WCF and Oracle.
In this example, we will develop a WCF RESTful service by using Oracle HR Sample Schema, ODP.NET, ADO.NET and Visual Studio 2015.
You can learn about the HR Schema in this post, I have written to introduce you to this schema.
The following illustration shows the components in the Employee service that I have written for this post.
Here a GET at http://localhost/WcfRest/EmployeeService.svc/102 show only one employee with the ID 102.