All about Web Service, SOAP, REST

Web service is a way to communicate between 2 machines via http, those 2 machines can be on different platform.

Lets see it 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.

Now come to Web Service.
Web services are of 2 types. SOAP and REST.

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.

REST - Representational State Transfer

Representation State Transfer. 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.

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.

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 Mathod

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 tag:

<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');



  • SOAP is XML based protocol. REST is an architectural style.
  • SOAP uses WSDL to communicate between client and server. REST uses XML and JSON to exchange data.
  • SOAP supports HTTP, SMTP, FTP protocols. REST supports HTTP protocol only.

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.



Top 8 C# 6.0 Features

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners