WCF REST
- GET : Get the resource (Records) from particular source such as SQL database.
- POST : Used to insert the records into the particular source such as SQL, Oracle database.
- PUT : Used to modify the resource or records.
- DELETE : used Delete the specific resource or record from particular source.
I hope you understand basic about REST concept , Now let us start step by step to create WCF REST Service.
Step 1: Create WCF Service.
Download Aspose : API To Create and Convert Files
To know how to create WCF service in depth please refer my article Creating WCF Service . So In this let see simple way to create WCF service:
- “Start” – “All Programs” – “Microsoft Visual Studio 2015”.
- “File” – “New Project” – “C#” – WCF Service Application as shown in below.
3 .Provide the project name such as “PayMentRESTService ” or another as you wish and specify the location.
4. Now delete the auto created Interface and svc file which we will create new one so beginners can understand it.
Step 2: Configure REST Service Template.
Now open the the IPaymentService.cs Interface file and write the following code:
using System.ServiceModel; using System.ServiceModel.Web; namespace PayMentRESTService { [ServiceContract] public interface IPayMentService { [OperationContract] [WebInvoke(Method = "GET",UriTemplate = "/PayBill/{PayId}", BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)] string PayBill(string PayId); } }
Let us understand above REST Template using following diagram
I hope you have understood the basic REST Template from above image.
Step 3: Implement IPaymentService.cs interface methods into PaymentService.svc.cs file as.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace PayMentRESTService { public class PayMentService : IPayMentService { public string PayBill(string PayId) { return "Transaction having PayId " + PayId + " was successful"; } } }
Now our REST Service Code is ready ,lest us complete another more steps.
Download Aspose : API To Create and Convert Files
Step 4: Configure End Points and Service Behaviors in web.config file as:
End Points and Service Behaviors configuration is very important in WCF Service ,many people saying it much complicated to configure but trust me its much easier and simple with powerful intellisense. Lets open web.config file and find system.serviceModel tag and follow below steps
Configure service behaviors as:
Configure End points as:
While configuring Endpoints Tag automatically shows how to set and what contract because it shows list of contract files (Interfaces) i.e. service contract .I hope you got basic idea about the End points configuration . Soon I will post video on this. After configuring Endpoints and service behaviors the system.serviceModel tag section of web.config file will be look like as follows:
<system.serviceModel> <behaviors> <serviceBehaviors > <behavior name="ServiceBehavior"> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="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 name="PayMentRESTService.PayMentService" behaviorConfiguration="ServiceBehavior"> <endpoint binding="webHttpBinding" contract="PayMentRESTService.IPayMentService" behaviorConfiguration="web"> </endpoint> </service> </services> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel>
I hope you have done same configuration which i have done .
Step 5: Test REST Service.
Now our service is ready .lets test it using REST client of Mozilla browser are as
Our REST Service URL will be http://localhost:64858/PayMentService.svc/PayBill/100
In the above Response of WCF REST service you have seen that status code is 200 OK. It means our service executed successfully .lets confirm by switching to Response Body tab of REST client as
From preceding output its clear that out service executed successfully and as per configuration its returned JSON output .Hope from preceding examples we have learned how to create WCF REST Service.