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.
- In Visual Studio 2013 we create 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 in the following.
- namespace 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 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).
- using 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)]
-
- string GetMessage();
-
- [OperationContract]
- [WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
- string PostMessage(string userName);
- }
- }
- In the final step we will implement the interface's functions GetMessage/Post Message as in the following:
- using 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 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:
- <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>
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