.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Jean Paul
SP
satyapriyanayak
Ashutosh Jha
Simon Robert VoIP

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Step by Step RESTfull service creation using WCF

Posted By:Mahesh       Posted Date: March 13, 2012    Points: 200    Category: WCF    URL: http://www.dotnetspark.com  

This article helps in Step by Step creation of RESTfull service using WCF service.
 

WCF most importantly includes elements for REST application are [WebGet] and [WebInvoke] attributes and the new binding webHttpBinding in connection with the webHttp endpoint behavior.

webHttpBinding endpoint enables you to choose from two different serialization formats:
•POX (Plain old XML) uses XML, without SOAP overhead.
•JSON (JavaScript Object Notation) very compact and efficient format, primarily in connection with JavaScript.

1) Create Data Contract

[DataContract]
public class Product
{
    [DataMember]
    public string ProductName { get; set; }
    [DataMember]
    public int Qty { get; set; }
    [DataMember]
    public double Rate { get; set; }
}

2) Define Service Contract
[ServiceContract]
public interface IProductService
{
    [OperationContract]
    [WebGet(UriTemplate = "/GetProductObj")]
    Product GetProduct();
       
    [OperationContract]
    [WebInvoke(UriTemplate = "/DeleteProduct/{ProductName}",
    Method = "DELETE")]
    void DeleteProduct(string ProductName);
}

3) Implement Service Contract
public class ProductService :
            IProductService
{
    public Product GetProduct()
    {
        Product stdObj = new Product {
                    ProductName = "Foo",
                    Qty = 2,
                    Rate = 100 };
        return stdObj;
    }

    void DeleteProduct(string ProductName)
    {
        // delete logic
    }
}

4) Use webHttpBinding (change default http port mapping to webHttpBinding) 

 
  
 

 

5) Specify webHttp End Point Behaviors

   
   
       
           
               
           

       

   

6) Test the Service
http://localhost/ProductService.svc/GetProductObj     
http://localhost/ProductService.svc/DeleteProduct/foo

Conclusion: In this way we can create a RESTfull service using WCF service.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend