RESTful Services in WCF

10 December 2021 at 10:00 by ParTech Media - Post a comment

In our previous blogs, we have read about WCF and RESTful services. We also saw the reasons for choosing them. For instance, WCF is known for its interoperability feature where it can communicate the responses in multiple formats and it can also adapt to various security protocols for communicating data.

On the other hand, REST is popularly known for being lightweight, easy to create and maintain. It operates mainly based on the defined HTTP protocols. More than this, the modern-day microservice architecture follows a RESTful services approach for implementing the services and communication between them.

Now, imagine you have a scenario where there is a core application in your organization that takes care of most of the business activities. Also, it is a WCF-based service that connects with multiple clients and sends data in different formats. There is another requirement to send responses with the REST protocol.

Creating an all-new API with all the business logic will be a duplication of work. Also, from a maintenance perspective, changes have to be done at both places. A temporary workaround solution before the business logic gets separated would be implementing a REST WCF service. Let’s understand about implementing the same in this article.

Table of contents

  1. Introduction to WCF REST
  2. Implementing WCF REST services
  3. Conclusion

Introduction to WCF REST

WCF stands for Windows Communication Foundation, it works based on Simple Object Access Protocol. REST stands for Representational State Transfer. REST supports cross-platform and exchange data in JSON and XML format and use the HTTP protocols (GET, POST, PUT, PATCH and DELETE)

GET is for fetching records from the data source while POST is for creating a new record in the data source. PUT is for updating an existing record from the data source, and DELETE is for deleting a record from the data source.

Let's now see the implementation of WCF in detail in the next section.

Implementing WCF REST Services

The idea is to implement REST service on top of the WCF framework. To achieve this, we are going to create a WCF-based service application first. Later we will implement the REST on top of it, and observe the response data.

First, create a new WCF Service Application project and provide a name for the solution. WCF is available with .Net Framework 4.7.2 and can be modified to any other framework version.

Once the project is created, the folder structure appears as below.

IService1.cs file and Service1.svc file are the main files that are responsible for exposing the endpoints from the application. IService1.cs file contains the operational contract details and the data contract details.

The operational contract is an interface that contains the names of the methods that get exposed from the service while the data contract is the custom data type that is used for communication with the endpoints.

Service1.svc implements the interface IService1.cs and usually holds the logic to call the respective business layer from here for completing the request.

To implement REST on top of WCF, we are going to use one of the available GET methods which are created along with the project. And also a new method for POST will be created. Let's now see how to communicate with them.

Before we start implementing, Open the IService1.cs file and modify the GetData method as below (remove the input parameter for easier understanding).

string GetData();

Next, open the Service1.svc file and modify the GetData method as below.

public string GetData()
​    {
​      return string.Format("You have reached the endpoint.");
​    }

The next step is to call this endpoint through the REST way. To achieve it, open the IService1.cs file on top of GetData() along with the OperationContract attribute. Add the below attribute info as well.

[WebGet(UriTemplate = "GetData", ResponseFormat = WebMessageFormat.Json)]

This indicates that this method is of type HTTP Protocol - GET. To call this, the endpoint address is ‘GetData’ and it returns the response of type JSON.

Users can provide the UriTemplate (endpoint name) of their choice. And can choose between JSON and XML response formats.

The next step is to configure the HTTP binding in the web.config to invoke the endpoint. The below lines need to be added in the web.config under the behavior element.

<endpointBehaviors>
​    <behavior name="PartechEndPointBehavior" >
​     <webHttp helpEnabled="true"/>
​    </behavior>
   </endpointBehaviors>

And the below lines need to be added to the system.servicemodel element directly.

<services>
   <service name="PARTECH_RESTWCF.Service1">
​    <endpoint address="" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="PartechEndPointBehavior"
​     contract="PARTECH_RESTWCF.IService1" />
   </service>
  </services>

Now, let's run the application and try to get the response from the endpoint.

Once the application is started, it loads up the compiled file structure of the application. On clicking the Service1.svc file where the WCF service is available for consumption, it provides the details on how to consume the service and get the WSDL file for it.

Now, let’s open the Postman tool for calling the endpoint. Change the HTTP protocol to GET and provide the URL - http://localhost:62088/Service1.svc/GetData (change the localhost:62088) to the corresponding port where it is hosted in the local machine.

Did you notice that the response data which we have configured in the Service1.cs file is getting printed in the response section?

Now, we will have a new endpoint - Create (POST - HTTP protocol).

To achieve it, add the below lines in IService1.cs file.

[OperationContract]

[WebInvoke(Method = "POST", UriTemplate = "CreateData", ResponseFormat = WebMessageFormat.Json)]

​    bool Create();

And in Service1.svc file,

 public bool Create()

​    {
​      return true;
​    } 

For the time being, we have just sent the response back as true. Users can have their actual business logic and process the information and send their response.

And to test it, open the postman and run the URL - http://localhost:62088/Service1.svc/CreateData with POST, did you see ‘true’ as the response?

Conclusion

In this post, we have implemented RESTful services in WCF applications, with which the existing business logic can be reutilized and used to connect with different clients.

Latest