Video: Creating WCF REST Service

This video tutorial guides you to create WCF REST service step by step which covers the following topics.
  • What is WCF REST Service ?
  • Creating WCF REST Service application.
  • Adding Service Contract.
  • Adding Operation contract.
  • Adding Data Contract
  • Configuring REST Template.
  • Understanding GET , PUT , POST methods
  • Configuring WCF endpoints in web.config file?
  • Returning JSON Data from WCF REST Service .
Now let's watch video



Don't Forget To  

Summary

I hope this tutorial is useful for all readers. If you have any suggestion, then please contact me.

Difference Between Web API and WCF REST Services

There are lots of talk on Web API and WCF REST regrading which one to use for REST Services . Many people are recommending Web API for REST service and they are saying stickily use Web API instead of WCF REST but personally I am not agree with it .The reasons are follows
  • We should not be stick on any particular technology i.e Web API or REST , It must  be decided  according to project requirement whether the Web API  or WCF REST is fit for  project requirement.
  • There are lots of technologies in market but first we need to analyze project scope and requirement and according to it we need to decide technology.
Lets see the difference between the Web API and WCF REST services based on the features

Scenario or Requirement
Web API
WCF REST
Need support of MSMQ.
No
Yes
Need duplex communication
No
Yes
Need End to end message security
No
Yes
Need Distributed transaction.
No
Yes
One way messaging.
No
Yes
Need to handle Long pooling requests
No
Yes
Need support of REST service in existing WCF Service
No
Yes
Need same code to be supported on both SOAP and REST
No
Yes
Need REST in ASP.NET MVC
Yes preferred
Yes
Need only HTTP service
Yes
Yes but not recommended
Open source
Yes
No

The above is the some differences according to the our project scenario and requirement.
Note 
 Learn WCF and REST Service on compilemode.com
Summary 
I hope this article is useful for all readers , If you have any suggestion regarding this article then please contact me.

Creating WCF REST Service Step by Step

In this video tutorial we will learn how to create WCF REST,tutorial prepared focusing on beginners to make them understand about the WCF REST Service.So we will learn it as step by step
In this video tutorial we will learn
  • Create WCF Project.
  • Adding Service and operation contract.
  • what is WCF REST Service. 
  • Understanding WCF REST Template .
  • What is WebInvoke and WebGet .
  • What URI Template
  • Request and Response message format.
  • Configuring End Points .
 Before start
 You will need  a basic understanding of WCF service, please read my following articles .
 I hope you have refereed it, If you wants to learn even more then please refer WCF Section
Watch Video 


Don't Forget To  

Summary
I hope this video tutorial is useful for all readers. If you have any suggestion then please contact me.

CRUD Operations In WCF REST with JSON Data

 Sometime we need to work with Json data in WCF REST services to make data available on across different platforms .so let us learn in this article how to make CRUD  operations in WCF REST service and communicate using Json data .To Perform CRUD operations in WCF REST service we need to use following HTTP methods 
  • 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.
To work with specific message format in WCF REST service,we need to set Web Message Format to Json or XML in REST template .Now let's implement above methods to make CRUD operations in WCF REST Service Step by step .

Prerequisites
To understand WCF REST service, you need to at-least beginner knowledge on WCF ,if you are beginner in WCF then please refer my following articles of WCF.
  1. Introduction to WCF Services
  2. Introduction To WCF Endpoints 
  3. Creating WCF Service
I hope you have read above articles .
Step 1: Create WCF Service as.
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2015".
  2. "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.


5. Now Add New WCF Service file and give name PayMentRESTService are as:
I hope you have followed the same steps and and learned how to add WCF Service. After adding Service file then the project solution explorer will be look like as follows


Step 2: Configure REST Service Template.

Now open the the IPaymentService.cs Interface file and write the following code for CRUD operation:
using System.ServiceModel;
using System.ServiceModel.Web;

namespace PayMentRESTService
{

    [ServiceContract]
    public interface IPayMentService
    {
        //To Insert or POST Records
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/AddPayee/{Name}/{City}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        void AddPayee(string Name, string City);

        //To Get Records from database 
        [OperationContract]
        [WebInvoke(Method = "GET",UriTemplate = "/PayBill/{PayId}", BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
        string PayBill(string PayId);

        //To Update records
        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "/UpdateBillPayment/{PayId}/{TransId}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        void UpdateBillPayment(string PayId,string TransId);

        //To delete records
        [OperationContract]
        [WebInvoke(Method = "DELETE", UriTemplate = "/RemovePayee/{Id}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        void RemovePayee(string Id);

    }
}

Let's understand code including REST Template.
  • IPayMentService : REST Service interface name
  • Method  : HTTP methods types it can be GET,POST,PUT,DELETE and other
  • UriTemplate : To Define url structure that how can be serice method accessed at client.
  • BodyStyle : Allows to define message body style format such as Bare, Wrapped etc.
  • RequestFormat : Defines in which message format does request come from client such xml or json.
  • ResponseFormat : Defines what  message format does service return to the client as a part of response such xml or json.
I hope you have got some idea about REST Template ..
Step 3: Implement IPaymentService.cs interface methods into PaymentService.svc.cs file as.

public class PayMentService : IPayMentService
    {
        public void AddPayee(string Name, string City)
        {
           //write database related insert logic here.
        }
        public string PayBill(string PayId)
        {
            return "Transaction having PayId " + PayId + " is successful";
            //write database related data retrival logic here.
        }

        public void RemovePayee(string Id)
        {
            //write database related delete logic here.
        }

        public void UpdateBillPayment(string PayId, string TransId)
        {
            //write database related update logic here.
        }
    }
Now our REST Service Code is ready for CRUD operation with JSON Data ,let's  complete another few steps.
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 browser extension are as
POST ( To insert records)
REST Service URL will be

http://localhost:64858/PayMentService.svc/AddPayee/vithal Wadje/Latur


In the above url last two parameters are input parameters .now test with REST client as

Using GET


Using PUT


Using DELETE

Hope from preceding examples we have learned how to Implement CRUD operations in WCF REST  Service .
Note:
  • Since this is a demo, it might not be using proper standards, so improve it depending on your skills.
  • CRUD Stands for create,Read,update and delete
Summary
I hope this article is useful for all readers. If you have any suggestion then please contact me.




WCF REST Service with JSON Data

Many times there is need to get the output in different message format such as XML,JSON or custom message format to fulfill the software requirement. So in this article we will learn how create WCF REST Service which return JSON format output .

Prerequisites
To understand WCF REST service, you need to at-least beginner knowledge on WCF ,if you are beginner in WCF then please refer my following articles of WCF.
  1. Introduction to WCF Services
  2. Introduction To WCF Endpoints 
  3. Creating WCF Service
I hope you have read above articles , now let us start with WCF REST from definition

 What is WCF REST ?
REST stands for Representational state transfer which is technique to communicate on cross platform application and exchange the data in JSON or XML format with the help of GET,POST,PUT,DELETE methods of HTTP protocol.

To return JOSN  as output we need to set RequestFormat of WebMessageFormat to JSON in REST template which we will learn in next.

let us briefly understand about the HTTP methods which is most commonly used to create WCF REST service as:
  • 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 JSON Service.

Step 1: Create WCF Service.

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:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2015".
  2. "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.

5. Now Add New WCF Service file and give name PayMentRESTService are as:
I hope you have followed the same steps and and learned how to add WCF Service. After adding Service file then the project solution explorer will be look like as follows


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.In the above example we set WebMessageFormat is JSON , So it will return the JSON format output.

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.
 
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.
Note:
  • Configure the database connection in the web.config file depending on your database server location.
  • Download the Zip file of the sample application for a better understanding.
  • Since this is a demo, it might not be using proper standards, so improve it depending on your skills.
Summary

I hope this article is useful for all readers. If you have any suggestion then please contact me.




How To Create WCF REST Service

I have read many forum post regarding WCF REST service creation and implantation but its much difficult to understand to beginners so to make it understandable, decided to write this article which shows step by step how create WCF REST Service.

Prerequisites
To understand WCF REST service, you need to at-least beginner knowledge on WCF ,if you are beginner in WCF then please refer my following articles of WCF.
  1. Introduction to WCF Services
  2. Introduction To WCF Endpoints 
  3. Creating WCF Service
I hope you have read above articles , now let us start with WCF REST from definition

What is WCF REST?

REST stands for Representational state transfer which is technique to communicate on cross platform application and exchange the data in JSON or XML format with the help of GET,POST,PUT,DELETE methods of HTTP protocol.

let us briefly understand about the HTTP methods which is most commonly used to create WCF REST service as:
  • 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.

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:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2015".
  2. "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.


5. Now Add New WCF Service file and give name PayMentRESTService are as:

I hope you have followed the same steps and and learned how to add WCF Service. After adding Service file then the project solution explorer will be look like as follows


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.
 
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.
Note:
  • Configure the database connection in the web.config file depending on your database server location.
  • Download the Zip file of the sample application for a better understanding.
  • Since this is a demo, it might not be using proper standards, so improve it depending on your skills.
Summary

I hope this article is useful for all readers. If you have any suggestion then please contact me.



www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode