How to Create WCF REST Service For Mobile App

This article explainss how to create WCF services for mobile applications.

Introduction

First allow me to say that this is my first article on c-sharpcorner.com.

In the era of smartphones and cloud throne, mobile applications now require not only beautiful interfaces and good quality but also the ability to synchronize data between devices using server-side technologies. If you have a plan to build applications like social apps, financial app.

You should care about technologies helping in building data service as soon as possible.

Currently there are many technologies in the .NET Framework to help you build services but in this article we will go into the details of the procedure to create a WCF REST Service that can be used on the mobile app or other client as desktop app, web app.

Create WCF REST service

In the first part we will create a WCF service that returns data in JSON format by 5 steps detailed as follows.

  1. In Visual Studio 2013 we create a new web project.



  2. Then add a new item to the project and select WCF Service Item.





  3. Delete the default generated code by Visual Studio and add two methods PostMessage, GetMessage as in the following.
    1. namespace Tungnt.NET.WCFRestDemo  
    2. {  
    3.     [ServiceContract]  
    4.     public interface IWCFRestDemo  
    5.     {  
    6.         [OperationContract]  
    7.         string GetMessage();  
    8.   
    9.         [OperationContract]  
    10.         string PostMessage(string userName);  
    11.     }  

  4. To use the WCF service as REST return JSON data we need to change the service interface's methods that has attributes to use WebGet (GET data only) or WebInvoke (for GET/POST/PUT/DELETE data) as in the following. (Note: These attributes are members of System.ServiceModel.Web.dll so we need to add this reference before use).




    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Runtime.Serialization;  
    5. using System.ServiceModel;  
    6. using System.ServiceModel.Web;  
    7. using System.Text;  
    8.   
    9. namespace Tungnt.NET.WCFRestDemo  
    10. {  
    11.     [ServiceContract]  
    12.     public interface IWCFRestDemo  
    13.     {  
    14.         [OperationContract]  
    15.         [WebGet(RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]  
    16.         //Like this: [WebInvoke(Method="GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
    17.         string GetMessage();  
    18.   
    19.         [OperationContract]  
    20.         [WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
    21.         string PostMessage(string userName);  
    22.     }  

  5. In the final step we will implement the interface's functions GetMessage/Post Message as in the following:
    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.   
    8. namespace Tungnt.NET.WCFRestDemo  
    9. {  
    10.     public class WCFRestDemo : IWCFRestDemo  
    11.     {  
    12.         public string GetMessage()  
    13.         {  
    14.             return "Welcome to tungnt.net from GetMessage() WCF REST Service";  
    15.         }  
    16.   
    17.         public string PostMessage(string userName)  
    18.         {  
    19.             return string.Format("Welcome {0} to tungnt.net from PostMessage() WCF REST Service", userName);  
    20.         }  
    21.     }  

Configure WCF REST Service

We've just created the WFC REST service but to use this service in a mobile app we need one more step. The configuration service must return a JSON format instead of the default format SOAP (XML). In this second part we will configure WCF to use webHttpBinding to enable a WCF service that returns JSON format. Let's edit the web.config as follows.

  • Add an endpoint used webHttpBinding and restBehavior (helpEnabled = true to serve the development process as will be explained below) as in the following:
    1. <system.serviceModel>  
    2.     <behaviors>  
    3.       <serviceBehaviors>  
    4.         <behavior name="">  
    5.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />  
    6.           <serviceDebug includeExceptionDetailInFaults="false" />  
    7.         </behavior>  
    8.       </serviceBehaviors>  
    9.     <endpointBehaviors>  
    10.       <behavior name="restBehavior">  
    11.         <webHttp helpEnabled="true"/>  
    12.       </behavior>  
    13.     </endpointBehaviors>  
    14.     </behaviors>  
    15.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"  
    16.         multipleSiteBindingsEnabled="true" />  
    17.     <services>  
    18.       <service name="Tungnt.NET.WCFRestDemo.WCFRestDemo">  
    19.         <endpoint name ="RESTEndPoint" contract ="Tungnt.NET.WCFRestDemo.IWCFRestDemo" binding ="webHttpBinding" address ="rest" behaviorConfiguration ="restBehavior"/>  
    20.       </service>  
    21.     </services>  
    22.   </system.serviceModel> 
    The WCF REST service is ready to be used, let's check the WCF REST service to ensure it can be used right now. Right-click on the WCFRestDemo.svc file and select View In Browser as in the following:



  • Add rest at the end of the browser's URL as configured in web.config endpoint address and hit Enter. You will see the results as shown below.



  • Click the service help page (as configured helpEnabled = true in web.config above) to see the rest of the methods in this service.


We are successful. We have just created and configured a WCF REST Service that can be used in a web app or mobile app.

Conclusion

In this article I showed you how to create a WCF REST service that returns JSON data to be used in web applications (via JavaScript) or mobile applications such as iOS, Android, Windows Phone app. JSON is a format that is very popular today in the world of Web/Mobile because of the simplicity, lightweight and high portability so understanding how to use it has become a strong demand for developers.

Hopefully this article will be helpful for you in the process of building your own applications. In the following article we will learn how to use this service on the Web, Windows Phone, Windows App. The detail article please read on the link below:


If you have any questions, experiences or ideas then please share in the comments below the article.

Happy coding. Stay tuned.

P.S.: You can download the example source code here: WCFRESTDemo