Consuming a Web Service using ASP.NET Ajax | WebReference

Consuming a Web Service using ASP.NET Ajax

By Joydip Kanjilal

A web service is a platform–independent software component that is based on the Simple Object Access Protocol (SOAP) and contains a group of functions that are packaged together for use in a common framework throughout a network. This article looks at how Web Services can be consumed using ASP.NET Ajax.

The three major components that make up a Web Service are:

  • A Web Service that runs on the Web Server
  • A Client application that invokes the Web Service
  • A WSDL document that describes the Web Service

Asynchronous JavaScript and XML (Ajax)

Before we delve deep into the implementation, let's have a quick recap of what Ajax is, and, why it's so popular. The word Ajax is an acronym for Asynchronous JavaScript and XML. It is a combination of different technologies, used for building rich and responsive user interfaces. Ajax is a popular technology that is used in many different ways on the World Wide Web. It has become a technology of choice for building fast and responsive user interfaces. According to Enrich Peterson, "AJAX-enabled pages provide a slick, responsive user experience, making web-based applications function more like desktop-based ones". The benefits of using Ajax include:

  • Faster page renderings and support for partial page updates
  • Rich and, responsive user interface
  • Reduced consumption of server resources

Implementing a Web Service in .NET

In this section, we will discuss how we can implement a simple web service in .NET. To implement a web service in Visual Studio, follow these steps:

1.  Open Visual Studio and click on File -> New Project.

2.  Select ASP.NET Web Service Application from the list of the templates displayed.

Figure 1

3.  Save the Web Service with a name.

Now if you look at the Solution Explorer, you will see a file called Service1.asmx created by default with the following content:

As you can see in the above code listing, HelloWorld is the default web method created when you create the web service. Web Methods are those methods that can be called using SOAP protocol. Now, replace the default web method with the following:

When you mark your web service with the [ScriptService] attribute, the ASP.NET AJAX framework would automatically generate a JavaScript proxy for your web service, which can then be used to call the web service methods. Here is how the complete source code for the web service would now look:

To test the web service you can type in the following URL in your web browser:

http://localhost/SimpleWebService/Service1.asmx

Here is how the web service looks when viewed in the web browser:

Figure 2

You can see the GetCustomerNames web method being displayed. When you execute the web method, here is how it looks:

Figure 3

Implementing the Web Service Client

In this section, we will implement a client application to consume the web service we just implemented in the earlier section. To do this, create an ASP.NET Application in Visual Studio and add the web service created earlier as a web reference. Now, take a ScriptManager control in your web form and specify the script reference as shown below:

You can now use simple JavaScript code to call the web method using Ajax. Here is the complete markup code in the Ajax enabled web form for your reference:

When you execute the application, the customer names are displayed in the web browser. Here is what the output looks like:

Figure 4

Summary

Web Services are Open Standard Enterprise Web Applications that are based on the SOAP protocol and are used for the efficient exchange of data on the same or disparate systems irrespective of the architecture. The MSDN states, "Web Services are based on a core set of standards that describe the syntax and semantics of software communication: XML provides the common syntax for representing data; the Simple Object Access Protocol (SOAP) provides the semantics for data exchange; and the Web Services Description Language (WSDL) provides a mechanism to describe the capabilities of a Web service." In this article, we discussed how we could implement a Web Service and then consume it using ASP.NET Ajax.

Download the source code for this article.