Microsoft > WCF >> Code Snippets Views : 16813
WCF to REST Service
Rate This Article :

Convert WCF Service to RESTFUL Service

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.

 WSDLScreenshot.png

Run and Test the RESTFUL Service:


1. To add the two numbers

 WCF_Addition.png            

2. Get Integer List

 WCF_INTLIST.png

3. Create List of User Detail

 WCF_UserList.png

 

About Author
Raj Kumar
Total Posts 55
Developer in .Net!
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
awr
daasdasd
  Privacy   Terms Of Use   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.