All about Web Service, SOAP and REST

In this blog we will see in details about Web Services, SOAP based web service and RESTful web service.
Let's start by exploring our first topic  - What is a Web Service?
Web service is a way to communicate between 2 machines via http, those 2 machines can be on different platform.
Let's understand this in details.
As you know any communication consists of medium and response. Same in web service , the medium of communication could be LAN, Internet using http. On another side response is based on input. In web service all communication format is XML based which we call it SOAP (Simple Object Access Protocol) message.

So there will be two object one will be client which will raise request (also consume the service) and a server which will give response (i.e. output) for raised request.

Web service collect all the details in an xml file called WSDL(Web Services Description Language).
This WSDL file must be access to client in order to access web service.

UDDI - UDDI stands for Universal Description Discovery and Integration. UDDI is a web directory for service provider who want to make their services available publicly.

Types of Web service

Web services are of 2 types. SOAP and REST.

What is SOAP?

SOAP - SOAP stands for Simple Object Access Protocol. SOAP web service are heavier than REST web  service. SOAP is xml based web service. SOAP contains below elements.
  • An envelop which defines the xml document. 
  • Header - It contains header information.
  • Body - It contains the response information.
  • Fault - It contains fault and error information.
SOAP has its own security known as WS-Security.

Few important points about SOA, SOAP and REST:

SOA is a software architecture where 2 components/machines communicate with each other.
One is consumer other is provider.

Web service is the implementation of SOA.

In web service, service provider publishes its service description to a directory and consumer can query against this directory which tells what services available and how to communicate.

WSDL - Web Service Description Language

WSDL is an xml document that describe web service. So service provider will communicate to service description using SOAP protocol. Consumer will query from service description using same protocol.

Advantages of SOAP web service

  • Inbuilt WS-Security
  • Language and Platform independent

Disadvantages of SOAP web service

  • As we all know SOAP is a xml based protocol, data exchange between 2 system happens only in xml format. XML data require parsing to read, so it is slow and consume more bandwidth.
  • Supports xml only as data exchange format

REST - Representational State Transfer

Now, we will try to make a point to understand What REST is?
Representation State Transfer - So, what does that mean? Simply, REST is just an architectural style to build and loosely couple 2 or more systems. In today's fast growing and connected IT world REST API is playing a vital role in web world.
REST says each resource should be accessed by URI.
In REST client sends a request and server responds for that request. This response is the representation of a resource which can be in HTML, XML, JSON, Plain Text format.

As we know REST is a style, so it can use any protocol like HTTP , SOAP.
REST is stateless, cache able.

Characteristics of RESTful web service

  • Fixed URI - Client server communicates over a given URI
  • Stateless - Server does not store any state.
  • Cacheable - Client can re-use response if it is cacheable

Advantages of RESTful web service

  • RESTful web service is faster than SOAP, RESTful web service consumes less bandwidth.
  • RESTful web service can also use SOAP, because SOAP is a protocol and REST is an architectural style.
  • Supports xml, plain text, json as data exchange format.

Advantage of REST over SOAP is that in REST client have direct accesses to a resource in form of URI, where in SOAP WSDL require to access service.

How to create a SOAP based Web Service?

To create SOAP web service follow below steps:

Open visual Studio, File->New Website
Once web site created in visual studio, Right Click on Project in Solution Explorer and select add new item. Select Web Service from given options, give a name MyWebWervice.asmx
Write below code in MyWebService.asmx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
public class MyWebService : System.Web.Services.WebService {

    [WebMethod(MessageName="Get By Employee ID")]
    public string GetEmployee(int employeeId)
        string empName = string.Empty;
        //write code and get employee name
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
        string query = "select employee_name,employee_department, employee_designation from employee where id = '" + employeeId + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();
       string empname = string.Empty;
        while (dr.Read())
            empname = dr["employee_name"].ToString();
        return empname ;

Now our SOAP web service is ready for consumption. To consume this service create a console application and name this project "SOAPClientDemo".

Right click on project and select "Add Service Reference", Enter web service url in address bar and click on GO. Your web service should appear below.

Change namespace if wish to change, and click on OK.

Below is the code to consume web service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SOAPClientDemo.ServiceClient;

namespace SOAPClientDemo
    class Program
        static void Main(string[] args)
            MyWebServiceSoapClient objClient = new MyWebServiceSoapClient();
            string name = objClient.GetEmployee(1);

Run this console application and it will display Employeed Name for Employee ID which will be passed in GetEmployee method.

SOAP web service can be secure to avoid unauthorized access, this can be achieve by custom SOAP header.

REST - Representational State Transfer. REST access web service through URI. REST web service includes 4 main method.

GET - To get or retrieve data.
POST - To create record
PUT - To update data
DELETE - To delete data

REST web service is less secure than SOAP web service as REST services are exposed over URI, but REST services are light weighted than SOAP web service.

Summary:  In SOAP request is sent in XMLformat, but REST uses HTTP request.
In SOAP XML request is processed but in REST parameters are processed.
SOAP returns XML response, REST returns JSON, Plain Text and XML format.
SOAP supports HTTP and SMTP as well.
REST can be consumed by by any client such as a browser by using AJAX and Java Script.

Some more info about Web Service:

Using Session in Web service:
Class must inherit from System.web.Services.WebService

and an attribute should be like this:

public void Do()

Web Method attribute properties:
Cache Duration - To cache the results of a web service method.
This is an interger values, which specify the no. of seconds.

Method Overloading in Web Method

Medthod overloading can be done based on parameter type, no. of parameters.

[WebMethod(MessageName="Add 2 numbers")]
public int Add(int a, int b)

[WebMethod(MessageName="Add 3 numbers")]
public int Add(int a, int b, int c)

Calling ASP.Net Web Service from Java Script using AJAX

Add below code:

<asp:scriptmanager id="SCriptManager1" runat="server">


<asp:ServiceReference Path="~/MyWebService.asmx"/>




function GetData()


var uid = document.getElementByID("txtUserID").value;

WebServiceNameSpace.WebUserServiceClass.WebMethod(id, OnSuccess, OnFail)


function OnSuccess(result)


document.getElementByID("txtValue").value = result["Value"];


function OnFail()


alert('There is an error');



Can SOAP be RESTful?

SOAP is a protocol so it can not use REST, whereas REST is an style and can use any protocol HTTP, SOAP etc.

SOAP can use HTTP, SMTP as transport protocols.

Differences between REST and SOAP | REST vs. SOAP

In below table you may find the differences between REST and SOAP.

Representational State Transfer
Simple Object Access Protocol
REST is an architectural style
SOAP is an XML based protocol
REST is lightweight and faster than SOAP
SOAP is a traditional protocol but not faster than REST
REST is stateless
SOAP is stateful
REST do not have a concept called WSDL
SOAP uses WSDL to describe web services



SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners

How to Crop and Upload image using JQuery