How to create WCF REST Service for Mobile App

Rate this post

In the era of smartphones and cloud throne, mobile applications now require not only beautiful interface and good quality but also the ability to synchronize data between devices using server side technologies. If you have plan to build applications like social apps, financial apps… You should care about technologies helping in building data service as soon as possible.

Currently there are lots of technologies in .NET Framework help you building service but in this article we will go into detail the steps to create 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 returns data in JSON format by 5 steps detailed as follow:

  1. In Visual Studio 2013 we created 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 below

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

  5. The final step we will implement the interface’s functions GetMessage/Post Message as below:

Configure WCF REST Service

We’ve just created WFC REST service but to use this service on mobile app we need one more step: Configuration service to return JSON format instead of the default format SOAP (XML). In this second part we will configure WCF to use webHttpBinding to enable WCF service returns JSON format. Let’s edit the web.config as follow:

  • Add an endpoint used webHttpBinding and restBehavior (helpEnabled
    = true to serve the development process as will be mentioned below)

WCF REST service have ready to use, let’s check WCF REST service can use or not right now. Right click on the WCFRestDemo.svc file and select View In Browser

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 service help page (as configured helpEnabled = true in web.config above) to see the rest of the methods in this service.


Succesful we have just created and configured WCF REST Service can be used in web app or mobile app.

Conclude

In this article I showed you how to create WCF REST service 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 knowledge for developer.

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 …

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

Happy coding. Stay tuned.

P/s: Example source code you can download here: WCFRESTDemo

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.