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:
- In Visual Studio 2013 we created a new web project
- Then add a new item to the project and select WCF Service Item
- Delete the default generated code by Visual Studio and add two methods PostMessage, GetMessage as below123456789101112namespace Tungnt.NET.WCFRestDemo{[ServiceContract]public interface IWCFRestDemo{[OperationContract]string GetMessage();[OperationContract]string PostMessage(string userName);}}
- 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)
1234567891011121314151617181920212223using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;namespace Tungnt.NET.WCFRestDemo{[ServiceContract]public interface IWCFRestDemo{[OperationContract][WebGet(RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]//The same: [WebInvoke(Method="GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]string GetMessage();[OperationContract][WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]string PostMessage(string userName);}} - The final step we will implement the interface’s functions GetMessage/Post Message as below:12345678910111213141516171819202122using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace Tungnt.NET.WCFRestDemo{public class WCFRestDemo : IWCFRestDemo{public string GetMessage(){return "Welcome to tungnt.net from GetMessage() WCF REST Service";}public string PostMessage(string userName){return string.Format("Welcome {0} to tungnt.net from PostMessage() WCF REST Service", userName);}}}
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="restBehavior"> <webHttp helpEnabled="true"/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="Tungnt.NET.WCFRestDemo.WCFRestDemo"> <endpoint name ="RESTEndPoint" contract ="Tungnt.NET.WCFRestDemo.IWCFRestDemo" binding ="webHttpBinding" address ="rest" behaviorConfiguration ="restBehavior"/> </service> </services> </system.serviceModel> |
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
[…] How to create a WCF Service for Mobile App […]