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
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
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
<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> |
Related blogs to WCF Service-
- Introduction to WCF
- ABC of WCF
- Basics of WCF Architecture
- WCF vs Web Service
- What is XML serialization?
- WCF Binding
- Create first WCF application
- Fault Contract in
- WCF Data Contract vs Message Contract
- Message Contract
- Data Contract Serialization and De-Serialization
- Data Contract in WCF
- Operation Contract in WCF
- Service Contract in WCF
- How to host a WCF service in IIS?
- WAS Hosting in WCF
- Self Hosting in WCF
- How to create WCF RESTful Service.
You may also see a video of this implementation on YouTube.
You may read some popular blogs on SharePointCafe.Net