How to create WCF RESTful Service.

In my earlier blog, I have explained about REST and SOAP.
In this blog, I will continue WCF tutorial sections. See a list of topic in WCF tutorial here.
REST is the short form of Representational State Transfer. REST says each resource should be accessed by URI.

How to create a RESTful WCF service.

First, create a WCF service application in Visual Studio and delete existing service and interface file.
Right-click on the project and add a new WCF service and name it ProductService.

Service Contract

WCf Restful Service


namespace WCFRESTServiceDemo
{
    // NOTE: You can use the “Rename” command on the “Refactor” menu to change the interface name “IProductService” in both code and config file together.
    [ServiceContract]
    public interface IProductService
    {
 
        [OperationContract]
        [WebGet(UriTemplate = “/GetProduct/{productId}”, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        List<Product> GetProductDetails(string productId);
    }
 
    public class Product
    {
        public string ProductId { get; set; }
        public string ProductName { get; set; }
        public string ProductCost { get; set; }
    }
}
 

 


Read this - What is Microservice Architecture?
Service Implementation

How to create WCf Restful Service


namespace WCFRESTServiceDemo
{
    // NOTE: You can use the “Rename” command on the “Refactor” menu to change the class name “ProductService” in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select ProductService.svc or ProductService.svc.cs at the Solution Explorer and start debugging.
    public class ProductService : IProductService
    {
        public List<Product> GetProductDetails(string productId)
        {
            Product objProduct = new Product();
            List<Product> objProductData = new List<Product>();
            objProduct.ProductId = productId;
            objProduct.ProductName = “Laptop”;
            objProduct.ProductCost = “1000 $”;
            objProductData.Add(objProduct);
            return objProductData;
        }
    }
}


Service Configuration

WCf Restful Service


<behaviors>
      <serviceBehaviors>
        <behavior name=ProductServiceBehavior>
          <!– To avoid disclosing metadata information, set the values below to false before deployment –>
          <serviceMetadata httpGetEnabled=true httpsGetEnabled=true/>
          <!– To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information –>
          <serviceDebug includeExceptionDetailInFaults=false/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name=Web>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration=ProductServiceBehavior name=WCFRESTServiceDemo.ProductService>
        <endpoint binding=webHttpBinding contract=WCFRESTServiceDemo.IProductService behaviorConfiguration=Web></endpoint>
      </service>
    </services>
How to create WCF RESTful Service Step by Step

Leave a Comment

RSS
YouTube
YouTube
Instagram