XML Web services created using ASP.NET Overview

This section introduces XML Web services created using ASP.NET. For more information on creating/exposing XML Web services, see the ASP.NET Quick Start. For more information on using XML Web services with Windows Forms see the Windows Forms and Data Access Quick Start.

The Internet is quickly evolving from today's Web sites that just deliver user interface pages to browsers to a next generation of programmable Web sites that directly link organizations, applications, services, and devices with one another. These programmable Web sites become more than passively accessed sites - they become reusable, intelligent Web Services.

The common language runtime provides built-in support for creating and exposing Web Services, using a programming abstraction that is consistent and familiar to both ASP.NET Web Forms developers and existing Visual Basic users. The resulting model is both scalable and extensible, and embraces open Internet standards (HTTP, XML, SOAP, WSDL) so that it can be accessed and consumed from any client or Internet-enabled device.

ASP.NET Web Services

ASP.NET provides support for Web Services with the .asmx file. An .asmx file is a text file that is similar to an .aspx file. These files can be part of an ASP.NET application that includes .aspx files. These files are then URI-addressable, just as .aspx files are.

The following example shows a very simple .asmx file.


<%@ WebService Language="VB" Class="HelloWorld" %>

Imports System
Imports System.Web.Services

Public Class HelloWorld :Inherits WebService

     <WebMethod()> Public Function SayHelloWorld() As String
          Return("Hello World")
     End Function

End Class
VB

This file starts with an ASP.NET directive WebService, and sets the language to C#, Visual Basic, or JScript. Next, it imports the namespace System.Web.Services. You must include this namespace. Next, the class HelloWorld is declared. This class is derived from the base class WebService; note that deriving from the WebService base class is optional. Finally, any methods that will be accessible as part of the service have the attribute [WebMethod] in C#, <WebMethod()> in Visual Basic, or WebMethodAttribute in JScript, in front of their signatures.

To make this service available, we might name the file HelloWorld.asmx and place it on a server called SomeDomain.com inside a virtual directory called someFolder. Using a Web browser, you could then enter the URL http://SomeDomain.com/someFolder/HelloWorld.asmx, and the resulting page would show the public methods for this Web Service (those marked with the WebMethod attribute), as well as which protocols (such as SOAP, or HTTP GET) you can use to invoke these methods.

Entering the address: http://SomeDomain.com/someFolder/HelloWorld.asmx?WSDL into the browser returns a Web Service Description Language (WSDL) document. This WSDL document is very important, and is used by clients that will access the service.

Accessing Web Services

In addition to the ASP.NET server side technology that allows developers to create Web Services, the .NET Framework provides a sophisticated set of tools and code to consume Web Services. Because Web Services are based on open protocols such as the Simple Object Access Protocol (SOAP), this client technology can also be used to consume non-ASP.NET Web Services.

Within the SDK, there is a tool called the Web Services Description Language tool (WSDL.exe). This command-line tool is used to create proxy classes from WSDL. For example, you could enter:

WSDL http://someDomain.com/someFolder/HelloWorld.asmx?WSDL

to create a proxy class called HelloWorld.cs.

This class would look very similar to the class created in the previous section. It would contain a method called SayHelloWorld that returns a string. Compiling this proxy class into an application and then calling this proxy class's method results in the proxy class packaging a SOAP request across HTTP and receiving the SOAP-encoded response, which is then marshaled as a string.

From the client perspective, the code would be simple, as shown in the following example.


Dim myHelloWorld As New HelloWorld()
Dim sReturn As String = myHelloWorld.SayHelloWorld()
VB

The return would be "Hello World".

The rest of this section deals with more advanced Web Services topics, such as sending and receiving complex data types. There is also a section on Text Pattern Matching, a technology that addresses any URI that returns text as if it were a Web Service. You can also perform data binding operations with Web Services; this topic is discussed in the Data section.


Copyright 2001 Microsoft Corporation. All rights reserved.