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
- 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.
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:
To test the web service you can type in the following URL in your web browser:
Here is how the web service looks when viewed in the web browser:
You can see the GetCustomerNames web method being displayed. When you execute the web method, here is how it looks:
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:
When you execute the application, the customer names are displayed in the web browser. Here is what the output looks like:
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.