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.