Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 1 - Introduction to asp.net web services

In this video, we will discuss 
1. Creating a simple asp.net web service
2. Technologies (HTTP, XML, SOAP) that are used with asp.net 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 them?
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 = "http://pragimtech.com/webservices")]

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 asp.net 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 asp.net AJAX.

Step 3: Copy and paste the following code in CalculatorWebServices.asmx
using System.Web.Services;
namespace WebServicesDemo
{
    [WebService(Namespace = "http://pragimtech.com/webservices")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class CalculatorWebServices : System.Web.Services.WebService
    {
        [WebMethod]
        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. You should see a page similar to the one below.
web services tutorial

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
http://www.w3.org/2003/06/soap11-soap12.html

asp.net webservices tutorial

5 comments:

  1. Very Good Tutorial to let me understand what is ASP.NET Web Services. Thanks man!

    ReplyDelete
  2. I am a .net developer. I learned web service,WCF and MVC completely from you tutorials. The way you analyse and the slide presentation and the teaching is really awesome and enough to learn. I really recommend everyone to follow this site. long live...

    ReplyDelete
  3. Hello Sir, your teachings say, viewstate restoration occurs in Init event, but when i tried to access, a viewstate variable, in InitComplete event, it didnot work, instead, i was able to access it in PreLoad event, please clear this confusion Sir

    ReplyDelete
  4. What a tutorial by you awesome by you.

    ReplyDelete
  5. HEY , I graduated as a programmer but I know nothing can you explain how can I expose services with existing wsdl file in c# .NET

    ReplyDelete

It would be great if you can help share these free resources