This article explains how to convert WCF to RESTFUL Service
in easy steps. To convert WCF to RESTFUL service, an operation contract
needs to be decorated with “WebInvoke (or) WebGet” to define the Request and Response
format as mentioned below. [WebInvoke(Method = "GET", UriTemplate = "GetUserList/{genCount}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] This WebInvoke attribute resides inside the “System.ServiceModel.Web” namespace is used to define the below
parameters at the end point to convert it to RESTFUL Service. 1. Service Method to
Invoke (POST/GET/PUT/DELETE) 2. URI template to invoke
using friendly URL 3. Request and Response
Format To start with create a new “WCF Service Application” and follow the below steps to modify the
WCF end point to JSON RESTFUL Service. Step 1: Modify the endpoint using WebInvoke (or) WebGet
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate
= "/Add/{number1}/{number2}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json)]
int Add(string number1, string number2);
[OperationContract]
[WebInvoke(Method
= "GET", UriTemplate = "GetIntList/{i}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List<int> GetListOfInt(string i);
[OperationContract]
[WebInvoke(Method
= "GET", UriTemplate = "GetUserList/{genCount}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<UserModel> CreateListOfUserDetail(string genCount); } Step 2:
(App.config/Web.config) In service App.Config/Web.Config,
modify the “Services” section by
mentioning the Binding, Contract and
behaviorConfiguration as mentioned
below. <service name="JSONRestService.Service1" behaviorConfiguration="servicebehavior"> <endpoint contract ="JSONRestService.IService1" binding ="webHttpBinding" address ="" behaviorConfiguration ="restbehavior"/> </service> Inside the “behaviors”
section, mention the “endpointBehavior”
configuration as “WebHTTP”. <endpointBehaviors> <behavior name="restbehavior"> <webHttp/> </behavior> </endpointBehaviors>
Additional
Information: <serviceMetadata httpGetEnabled="true"/>
If setting the above attribute to “true”, wsdl generation link will be shown in the browser.
Run and Test the RESTFUL Service:
1. To add the two
numbers
2. Get Integer List
3. Create List of
User Detail
|