WCF RESTful Service

Introduction

I have read several WCF Rest services articles and found that it is difficult to understand the concept for new learners, hence I am trying to create simple WCF REST Service sample and going to explain how it is going to access the service using windows application.

RESTful service follows the REST (Representational State Transfer) architectural style. WCF service will allows to make calls and exchange the data using SOAP protocol over different protocols (HTTP, TCP, MSMQ etc..) and it uses the complex mechanism like SOAP for communication.

The REST service is simple to use it uses the HTTP protocol for all operations, its lightweight and faster, it uses the .json and .xml formats.

Creating Sample application for WCF REST

  1. Open Visual Studio and create new WCF Service Application

    Application
    service

    It will create 2 files (Interface and Service), the interface defines the method names as operation contracts, and we can define all our methods here.

    It also contains the DataContracts in which we can define our Data members.
    1. namespace RestFullServiceSample  
    2. {  
    3.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.  
    4.     [ServiceContract]  
    5.     public interface IService1  
    6.     {  
    7.         [OperationContract]  
    8.         [WebGet(UriTemplate = "Students", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]  
    9.         List <Student> GetStudentDetails();  
    10.   
    11.         // TODO: Add your service operations here  
    12.     }  
    13.   
    14.   
    15.     // Use a data contract as illustrated in the sample below to add composite types to service operations.  
    16.     [DataContract]  
    17.     public class Student  
    18.     {  
    19.         [DataMember]  
    20.         public string ID { getset; }  
    21.         [DataMember]  
    22.         public string Name { getset; }  
    23.     }  
    24. }  
    Above example define function called GetStudentDetails, it uses the WebGet property to do the REST Service, the WebGet property use to get the details, we can also use the WebInvoke method to do the CRUP operations.

    UriTemplate:

    REST service mainly depending on the service URL to fetch the data, we can define any name here and the service can access using the URI.

    ResponseFormat and RequestFormat:

    It defines in which format data should be transformed, we can select XML or JSON formats here.

  2. Implement this interface methods in service file (Service1.svc file).
    1. public class Service1 : IService1  
    2.     {  
    3.         public List<Student> GetStudentDetails()  
    4.         {  
    5.             List<Student> stuList = new List<Student>();  
    6.             stuList.Add(new Student { ID = "123", Name = "Ramakrishna" });  
    7.             return stuList;  
    8.         }  
    9.     }  
    I have added my own custom code to return the student details.

  3. Configure the Service behavior and endpoint behavior in web.config file

    Configure
    Configure
    1. <system.serviceModel>  
    2.     <services>  
    3.       <service name="RestFullServiceSample.Service1">  
    4.         <endpoint address="" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="RFEndPointBehavior"  
    5.           contract="RestFullServiceSample.IService1" />  
    6.       </service>  
    7.     </services>  
    8.     <behaviors>  
    9.       <endpointBehaviors>  
    10.         <behavior name="RFEndPointBehavior"  >  
    11.           <webHttp helpEnabled="true"/>  
    12.         </behavior>  
    13.       </endpointBehaviors>       
    14.     </behaviors>  
  4. Browse the service and test whether you are able to access the student details using web URI which is mentioned in the interface file.

    interface
    student

  5. Create Windows application to access the WCF Rest service.

     WCF Rest service

  6. Access the Rest service methods using its url.
    1. private void btnBind_Click(object sender, EventArgs e)  
    2.         {  
    3.             WebClient proxy = new WebClient();  
    4.             proxy.DownloadStringAsync(new Uri("http://localhost:1678/Service1.svc/Students"));  
    5.             proxy.DownloadStringCompleted +=proxy_DownloadStringCompleted;  
    6.         }  
    7.   
    8.         void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)  
    9.         {  
    10.             Stream stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));  
    11.             DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(List<Student> ));  
    12.             List<Student> result = obj.ReadObject(stream) as List<Student>;  
    13.   
    14.               
    15.             label1.Text = result[0].ID.ToString();  
    16.             label2.Text = result[0].Name.ToString();  
    17.               
    18.         }  
  7. Run the windows application and call the WCF Rest service,

    windows application

    Hope this article help you in understanding the simple WCF Rest service.
Read more articles on WCF:

Up Next
    Ebook Download
    View all
    Learn
    View all