Understanding RESTFul (POST, PUT and DELETE) services with Windows Communication Foundation (WCF) and Oracle XE.

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. HTTP besides GET, the HTTP protocol supports other forms of verbs such as POST, PUT, and DELETE, which you can use in a REST service to create, modify, and remove resources, respectively. Using these verbs you can build WCF services that can insert, update, and delete data.

The good practice is that you use HTTP POST requests to specify operations that can add new records, HTTP PUT requests for operations that update existing data, and HTTP DELETE requests to define operations that can remove records.

POST is an exception in certain regards. POST is frequently misused as DELETE and PUT, because the use of DELETE and PUT is either not permitted or technically impossible from the browser’s perspective, and you could use HTTP POST requests to update and delete data.

Use the [WebInvoke] attribute for scenarios POST, PUT and DELETE, you use this attribute to identify a URI, but you can also indicate the type of the request message to which to respond.

In the following example, I write a REST WCF Service to enable insert, update and delete operations for Oracle HR Schema.

You can learn about the HR Schema in this post.

The following table shows the URIs and the parts of the interface that I will implement for each URI in the example.

URI Method Output Input
/employees POST bool An Employee Object
/employees/{id} PUT bool An employee Object with id specified.
/employees/{id} DELETE bool An employee Id

The main steps for this exercise are as follows:

  1. Use the EmployeesDac class, which contains the method for accessing the database.
  2. Write the EmployeesServiceImplementation class with the following code (fig 1).
    Fig 1. EmployeeServiceImplementation.cs
  3. Write a new interface called IEmployeesServiceContract and type the following code(fig 2).
    Fig 2. IEmployeeServiceContract.cs
  4. Write the EmployeesService.svc file that references the service implementation with the following code (fig 3).
    Fig 3. EmployeeService.svc
  5. Finally, add the following config file (fig 4)
    Fig 4. Web.config

Testing the service with Soap UI.

The WCF service that you have built runs the same way as a regular Web application and is
hosted by a Web Server.

If you browse the .svc file, you can view the help page for the WCF service. It verifies that the WCF
service has been configured correctly (you will see error messages if the WCF service cannot start) and
provides information showing how you can connect to the service.

Once we’ve made all the required settings, running the tests are very easy with SOAP UI. Before running, we can
define the json request or query string parameters. Use the Green button to start running the test.

Testing the HTTP-POST request, after completing the execution, the result window displays the JSON Response.
Fig 5. HTTP-POST Request
Testing the HTTP-PUT request after completing the execution, the result window displays the JSON Response.
Fig 6. HTTP-PUT Request
Testing the HTTP-DELETE request after completing the execution, the result window displays the JSON Response.
Fig 7. HTTP-DELETE Request

Download example source code.

Implementing a RESTFul service with Windows Communication Foundation (WCF) and Oracle HR Schema

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.

Fig 1. Components of our Employee RESTFul service.
Step 1:Write the POCO object.
Fig 2. Employee entity class.
Step 2: Write the following utility class.
Fig 3. Utility class.
Step 3: Write the following helper class adding the Oracle Data Provider for .NET.
Fig 4. DAC data access class.
Step 4:Define and write the service contract.
Fig 5. Service contract interface.
Step 5: Write the service implementation class.
Fig 6. Service implementation class.
Step 6: Write the following Employee.svc file. Use the WebServiceHost class, the WebServiceHost class inherits from ServiceHost and automatically assigns the correct binding and behavior to your endpoint. You no longer need to be concerned about the content of your configuration file.
Fig 7. EmployeeService.svc File.
Step 7: Write the configuration file and store the connection string for the Oracle HR schema.
Fig 8. Configuration File.

In this example a GET at http://localhost/WcfRest/EmployeeService.svc/Employees shows all the employees in the HR database.
Fig 9. Running the example, querying all the employees.

Here a GET at http://localhost/WcfRest/EmployeeService.svc/102 show only one employee with the ID 102.

Fig 10. Querying only one employee.
Also, a GET at http://localhost/WcfRest/EmployeeService.svc/110 show the employee with the ID 110.
Fig 11. Querying another employee.

Download example source code.