A Web service is an application that exposes functionality to clients, providing some service or access to data in a controlled manner. In many ways, Web services are analogous to the Distributed Component Object Model (DCOM) objects that many Microsoft developers are used to working with. The main difference, however, is that Web services are accessed through standards-based, open interfaces such as SOAP and HTTP.
As Web services are created, they’ll be “owned” by the experts who develop them, whether the services are publicly available or private. These services can be called by applications on your local network or from across the Internet. Commands and messages will be received from client applications, and the results can be returned to the calling application. The applications can be written in any language and hosted on any operating system.
A .NET Web service is a Web application that consists of a number of pages, or access points, that are used to call functions. When you create a Web service project, the Microsoft Visual Studio .NET integrated development environment (IDE) creates a virtual Web directory that holds the project files. This section briefly describes the makeup of a Web service.
A Web service can include a number of access points, each defined by an .asmx file. This file is used to describe the Web service class that’s being called. Within the file is a single WebService directive that identifies the page as a Web service, the language used to code the service, the location of the code for the Web service, and the Web service class name, as shown here:
<%@ WebService Language="c#" Codebehind="Service1.asmx.cs" Class="WebService1.Service1" %>
The application code can then be found within the page identified by the Codebehind attribute of the directive.
Within the code file that the Codebehind attribute of the WebService directive identifies, a Web service class will be defined. This class will be the primary access point for this particular Web service component. It is identified by the Class attribute in the WebService directive and will inherit the System.Web.Services.WebService class, as shown here:
public class Service1 : System.Web.Services.WebService { //All of the Web service code }
By using this as the base class, the Web service will gain access to the standard Microsoft ASP.NET objects, including the following:
Application Holds application-level data, available to all users
Server Contains useful Web service utility functions and data
Session Holds user-specific data and session information
User Accesses information about the current user, if the user is authenticated
From the Web service class, you’ll develop the code to support the functions you’ll need. You’ll write functions as you would in any other application so that they can be available to the Web service clients.
When you use the Web service class as an access point, you can provide further documentation for your Web service. This documentation will be made available when the component is accessed through the discovery process, by accessing the Web service page and passing it the wsdl parameter (http://localhost/WebService1/Service1.asmx?wsdl). See the section “Identifying Web Services Using Discovery,” later in this chapter, for a detailed description of the discovery process. In the Visual Studio .NET IDE, when you add the Web reference and click the View Documentation link, you’ll see a dialog box similar to the one shown in Figure 21-1.
To provide this additional documentation, you’ll attach the WebService attribute to the Web service class by adding a tag to the line prior to the class declaration, as shown in the following code. The WebService attribute provides documentation based on the property settings that you supply.
[ WebService( Description="This is a sample Web service, with default names.", Namespace="http://www.mysite.com/", Name="My Web Service Name" ) ] public class Service1 : System.Web.Services.WebService { //All of the Web service code }
The properties for the WebService attribute can include the following:
Description Provides a description of the Web service
Name Specifies a name for the Web service
Namespace Specifies an XML namespace for the Web service
Any public method in the Web service class can be made available directly through the Web service. To make a method available, you simply add a line prior to the method definition, designating the method as a Web service method. You’ll do this using the WebMethod attribute, as shown here:
[WebMethod( Description="A sample WebMethod that returns 'Hello World'", EnableSession=false)] public string HelloWorld() { return "Hello World"; }
This attribute can be expanded with the following properties, which give more information about the method:
BufferResponse Specifies whether the response will be buffered.
CacheDuration Specifies the number of seconds the response will be held in the cache.
Description Provides a description of the method, for documentation.
EnableSession Specifies whether the method retains session state information.
MessageName Specifies the name used for the Web service method in the data being passed to and from the method. The default is the name of the method.
TransactionOption Specifies whether the method supports transactions.