REST API Using WCF Service

We will not go into the details about what REST architecture style is. Depending on it's type, our service will be of the nature where each method will act as a resource on the internet. So to create a REST based service, we will create a new ASP.Net project and add a WCF service project into it. Let's call it RESTService.

WCF Service

Next, we remove any default methods. We add a sample class named POCOClass with the 2 properties Name and Id. Next we add a method to the service that returns a list of the type POCOClass.

remove any default methods

Next, to make this service a RESTful type, we need to do some code changes as in the following:

  1. Add the WebGet attribute or WebInvoke attribute to the methods to allow them to be used with the HTTP verbs like GET, PUT, POST and so on.

  2. Add a URI template to these methods that is nothing but a URL that will identify the methods as a unique resource on the internet.

  3. Change the binding to be used as a webHttpBinding type.
So let's start by adding the WebGet attribute on the method. To use these attributes, we need to add a reference for System.ServiceModel.Web.

adding the WebGet

Add the WebGet attribute on the implementation of the service method.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7. using System.Net;  
  8. using System.ServiceModel.Web;  
  9. using System.Runtime.Serialization.Json;  
  10. using System.IO;  
  11. using System.Web.Http;  
  12. namespace SampleApplication {  
  13.     public class RESTService: IRESTService {  
  14.         [WebGet]  
  15.         List < POCOClass > IRESTService.GetListData() {  
  16.             var _lstPOCOClass = new List < POCOClass > ();  
  17.   
  18.             _lstPOCOClass.Add(new POCOClass {  
  19.                 Id = 1,  
  20.                 Name = "User 1"  
  21.             });  
  22.   
  23.             _lstPOCOClass.Add(new POCOClass {  
  24.                 Id = 2,  
  25.                 Name = "User 2"  
  26.             });  
  27.   
  28.             _lstPOCOClass.Add(new POCOClass {  
  29.                 Id = 3,  
  30.                 Name = "User 3"  
  31.             });  
  32.             return _lstPOCOClass;  
  33.         }  
  34.     }  
  35. }  
Next, to make it as a resource on the web, we use the UriTemplate parameter to the WebGet attribute above. We also add a parameter named ResponseFormat, to make it return a JSON type.
  1. [WebGet(UriTemplate = "/GetListData", ResponseFormat = WebMessageFormat.Json)]  
  2. List < POCOClass > IRESTService.GetListData() {  
  3.     var _lstPOCOClass = new List < POCOClass > ();  
  4.   
  5.     _lstPOCOClass.Add(new POCOClass {  
  6.         Id = 1,  
  7.         Name = "User 1"  
  8.     });  
  9.   
  10.     _lstPOCOClass.Add(new POCOClass {  
  11.         Id = 2,  
  12.         Name = "User 2"  
  13.     });  
  14.   
  15.     _lstPOCOClass.Add(new POCOClass {  
  16.         Id = 3,  
  17.         Name = "User 3"  
  18.     });  
  19.   
  20.     return _lstPOCOClass;  
  21. }  
Now we need to add settings for making it use webHttpBinding.

First, we need to change the endpoint behavior to use the webHttpBinding. This is to be done inside:

system.serviceModel => behaviors => endpointBehaviors => behavior

and change the endpoint to use the webHttpBinding. This is to be done in:

system.serviceModel => services => service => endpoint

The complete settings will look like:
  1. <configuration>  
  2.     <system.web>  
  3.         <compilation debug="true" targetFramework="4.5" />  
  4.         <httpRuntime targetFramework="4.5" />  
  5.     </system.web>  
  6.     <system.serviceModel>  
  7.         <behaviors>  
  8.             <serviceBehaviors>  
  9.                 <behavior name="">  
  10.                     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />  
  11.                     <serviceDebug includeExceptionDetailInFaults="false" />  
  12.                 </behavior>  
  13.             </serviceBehaviors>  
  14.             <endpointBehaviors>  
  15.                 <behavior name="web">  
  16.                     <webHttp/>  
  17.                 </behavior>  
  18.             </endpointBehaviors>  
  19.         </behaviors>  
  20.         <services>  
  21.             <service name="SampleApplication.RESTService">  
  22.                 <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"  
  23. contract="SampleApplication.IRESTService" ></endpoint>  
  24.             </service>  
  25.         </services>  
  26.         <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  27.     </system.serviceModel>  
  28. </configuration>  
That's it. We are done with the settings and now we simply need to test it. To do this, we will browse to the service to get the URL.

rest service

Next, we use the Google Chrome extension app called Postman to test the service. Add the service URL and select GET as the HTTP verb.

Add the service

Click the Send button and see the results.

Click the Send button

Next, let's try to POST some data to it. So we add another method that receives data of the type POCOClass from the HttpBody. We also add the WebInvoke attribute and the UriTemplate for it.
  1. [WebInvoke(UriTemplate="/PostUserData", RequestFormat=WebMessageFormat.Json)]  
  2. void IRESTService.PostUserData([FromBody] POCOClass userData)  
  3. {  
  4.   
  5. }  
Now again, we use our POSTMan app to POST the data to the same URL, but with the method name as defined in the URI template.

POSTMan

Click Send and see the results. We are getting the results on the server.

Click Send

Easy, isn't it? So create the REST services depending on the requirements. No need to use the SOAP based WCF service, making it lighter.


Similar Articles