Take advantage of WCF to build light-weight RESTful services and keep the resource URIs clean and lean.

WCF (Windows Communication Foundation) is a secure, reliable, and scalable messaging platform that can be used to build Web services in .Net. It provides a unified programming model for developing service oriented applications.

You can use WCF to build RESTful services in .NET. REST (Representational State Transfer) is an architecture paradigm that conforms to the REST architecture principles.The REST architecture is based on the concept of resources: It uses resources to represent the state and functionality of an application. These resources are in turn identified using URIs over the HTTP protocol.

Creating a WCF service

In this section we will explore how we can build a RESTful service in WCF. First off, let’s create a new WCF service in Visual Studio. To do this, follow the steps outlined below. Note that to build the application illustrated in this article I have used Visual Studio 2015 although you can use Visual Studio 2012 or 2013 as well.

  1. Open Visual Studio 2015
  2. In the File menu in the Visual Studio IDE, click on Start -> File -> New -> Project
  3. Next, select WCF from the list of the project templates displayed
  4. Select “WCF Service Application” on the right side pane
  5. Specify a name for your WCF service project and click OK to save it

This would create a new WCF Service Application project in the name you specified. The project would also contain a default service for illustration purposes only.

Implementing the RESTful WCF Service

When working with WCF, you first need to create a service contract and then define the service operations or operation contracts in it. Typically, a WCF service comprises of the following:

  1. Service class
  2. Service contract
  3. One or more operation contracts
  4. One or more endpoints
  5. Hosting environment

A ServiceContract is used to specify the operations that are available for the service client to consume.  The following code snippet shows how a service contract looks like — we will modify this later to make it RESTful.

A DataContract is used to describe the data that needs to be exchanged between the service provider and the service consumer. Consider the following DataContract called Customer.

An operation contract is used to expose a method as a service method and also transaction flow, direction of the service operation and also the fault contract(s) that may be associated. The following code snippet illustrates how you can declare a service operation using the OperationContract attribute and the use the WebInvoke attribute to specify the HTTP operation, Uri, Web message format.

The following code snippet illustrates how the customer service can be made RESTful by applying the WebInvoke attribute on its service method.

The CustomerService class extends the ICustomerService service contract and provides the implementation of the service operation named GetCustomerList. Here’s how the CustomerService class would look like.

Note that the PopulateCustomerData method is not a service method; it’s a private method that returns a list of customer records and is called from the GetCustomerList service method.

The next thing you should do is configure the WCF service. To do this, you would need to specify the binding and endpoint details and also the service behavior. The following code snippet shows how the service configuration should look like for this service.

And that’s all you have to do. You can now open a Web browser and test your WCF RESTful service.

5/5 - (2 votes)