banner



How To Create Wcf Rest Service

  • Updated date Aug 28, 2019
  • 149.7k
  • 5

In this article you will learn about 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.     [ServiceContract]
    4. public interface  IService1
    5.     {
    6.         [OperationContract]
    7.         [WebGet(UriTemplate ="Students" , ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
    8.         List <Student> GetStudentDetails();
    9.     }
    10.     [DataContract]
    11. public class  Student
    12.     {
    13.         [DataMember]
    14. public string  ID { get ; set ; }
    15.         [DataMember]
    16. public string  Name { get ; set ; }
    17.     }
    18. }
    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. void  proxy_DownloadStringCompleted( object  sender, DownloadStringCompletedEventArgs e)
    8.         {
    9.             Stream stream =new  MemoryStream(Encoding.Unicode.GetBytes(e.Result));
    10.             DataContractJsonSerializer obj =new  DataContractJsonSerializer( typeof (List<Student> ));
    11.             List<Student> result = obj.ReadObject(stream)as  List<Student>;
    12.             label1.Text = result[0].ID.ToString();
    13.             label2.Text = result[0].Name.ToString();
    14.         }
  7. Run the windows application and call the WCF Rest service,

    windows application

    Hope this article helps you in understanding the simple WCF Rest service.

Read more articles on WCF:

  • WCF Interview Questions and Answers
  • Implementing a Basic Hello World WCF Service

How To Create Wcf Rest Service

Source: https://www.c-sharpcorner.com/article/wcf-restful-service/

Posted by: blakeronfiess.blogspot.com

0 Response to "How To Create Wcf Rest Service"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel