Part 1 Introduction to asp net web services

Link for code samples used in the demo

Link for all dot net and sql server video tutorial playlists

In this video, we will discuss
1. Creating a simple web service
2. Technologies (HTTP, XML, SOAP) that are used with web services

Please Note: ASMX web services are a legacy technology. Most of the companies are now using WCF (Windows Communication Foundation) to build XML Web services and XML Web service clients. However, this course will be useful for those who are searching for a job as a dot net developer as there are some companies still using ASMX web services today, and could ask interview questions related to them.

In WCF video series we will discuss building XML Web Services and clients.

What are web services and why should we use web services?
Web services are a standardized way for developing interoperable applications i.e enabling an application to invoke a method of another application. These applications can be on the same computer or different computers. Web services use open standards and protocols like HTTP, XML and SOAP. Since these are open and well known protocols, applications built on any platform can interoperate with web services. For example, a JAVA application can interoperate with a web service built using .NET. Similarly a web service built using JAVA can be consumed by a .NET application.

Hyper Text Transfer Protocol (HTTP) is the protocl widely used by web services to send and receive messages.
The messaging protocol is SOAP. SOAP stands for Simple Object Access Protocol. SOAP messages are in XML format.

Building ASP.NET web services
Building ASP.NET web services is easy. Here are the steps.
Step 1: Create a an ASP.NET Empty Web Application and name it WebServicesDemo.

Step 2: Right click on WebServicesDemo project in solution explorer and add – New Item. From the Add New Item dialog box select Web Service. Change the name of the WebService1.asmx to CalculatorWebServices.asmx.

Web Services have .asmx extension. For this reason web services are also often called as ASMX web services.

Notice that a webservice is a class that is decorated with [WebService] attribute and inherits from System.Web.Services.WebService base class. The [WebService] attribute tells that the this class contains the code for a web service. WebService Namespace is used to uniquely identify your web service on the internet from other services that are already there on the Web. WebService Namespace can be any string, but it is common to give it a company’s internet domain name as they are usually unique. Something like
[WebService(Namespace = “”)]

It is not mandatory for a web service to inherit from System.Web.Services.WebService base class. However, if the web service has to use ASP.NET session or application state objects, then inheriting from System.Web.Services.WebService base class will provide direct access to these objects.

To allow a web service to be called from Javascript, using ASP.NET AJAX, then decorate the web service class with [System.Web.Script.Services.ScriptService] attribute. In a later video session, we will discuss calling a web service using AJAX.

Step 3: Copy and paste the following code in CalculatorWebServices.asmx
using System.Web.Services;
namespace WebServicesDemo
[WebService(Namespace = “”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CalculatorWebServices : System.Web.Services.WebService
public int Add(int firstNumber, int secondNumber)
return firstNumber + secondNumber;

Notice that, the CalculatorWebServices class contains a single method called Add(). This method adds 2 numbers and return the sum. This method is decorated with [WebMethod] attribute. If you want the method exposed as part of the Web service, then the method must be public and should be decorated with [WebMethod] attribute. This attribute has got several properties which can be used to configure the behavior of the XML Web service method. We will discuss these properties in a later video session.

At this point we are done building a web service. Run the application by pressing CTRL + F5.

Notice that the Add() method that we created in the web service is displayed on the page. Clicking on the method name will take you to page where you can test the method. This page also shows the format of the SOAP request and response messages.

SOAP 1.2 is a new version with some major changes. For all the differences between them please refer to the following link