Chapter 19. Introduction to XML Web Services

Microsoft .NET XML Web services are supported by Microsoft Internet Information Service (IIS) versions 5.0 and later. It’s important that IIS be already installed on a machine before the .NET installation is performed: the .NET installer recognizes that IIS is installed and establishes the bindings that allow Microsoft ASP.NET and XML Web services to function. If IIS is installed later, the .NET Framework must be removed and reinstalled.

The XML Web service support in .NET is provided by ASP.NET, a component of .NET that is outside the scope of this book. ASP.NET is used by IIS to invoke XML Web services; however, the details and features of ASP.NET aren’t required to write XML Web services.

Visual Studio .NET includes extensive support for working with XML Web services, including tight integration with IIS; for more information, consult the Visual Studio documentation. In this chapter, however, we demonstrate how to build and deploy XML Web services using the command-line tools included with the .NET SDK.

The .NET support for XML Web services abstracts building and deploying XML Web services from the underlying standards and protocols; however, for clarity, Table 19-1 includes simple definitions of the key technologies.

Table 19-1. Key Elements of the .NET Framework XML Web Services

Technology

Description

XML Web services

A component that exposes functionality through Internet protocols.

Simple Object Access Protocol (SOAP)

An XML-based protocol for exchanging structured and typed information on the Web. Messages between clients and XML Web services are usually encoded using SOAP.

Web Services Description Language (WSDL)

An XML-based contract language that defines the services offered by an XML Web service.

Universal Description, Discovery, and Integration (UDDI)

A protocol for advertising the existence, location, and features of XML Web services.

ASP.NET

An element of the .NET Framework used to build server-based Web applications.

Creating XML Web Services

XML Web services are created by using the WebService and WebMethod attributes defined in the System.Web.Services namespace. The following example demonstrates a simple class annotated to be an XML Web service, providing a method that adds two integers:

using System;
using System.Web.Services;

namespace SumService {

    [WebService(Namespace="http://mycompany.com")]
    public class Sum {

        [WebMethod]
        public int sumNumbers(int p_first_number,
            int p_second_number) {
            return p_first_number + p_second_number;
        }
    }
}

The WebService attribute is optional and is used to describe the service that will be offered to clients. The most important property of this attribute is Namespace, which is used to define a unique name for the XML Web service endpoints (the methods that are exposed). The Namespace property defaults to http://tempuri.org and should be changed before an XML Web service is released publicly; in our working example, we set the namespace to be http://mycompany.com.

The properties defined by the WebService attribute are listed in Table 19-2.

Table 19-2. The WebService Properties

Property

Description

Description

A descriptive message for the XML Web service

Name

The name of the XML Web service

Namespace

The default namespace for the XML Web service

The WebMethod attribute is applied to methods that should be made available as part of the XML Web service; only the methods that need to be available to XML Web service clients should be annotated. The WebMethod attribute can be applied only to instance methods; static methods, properties, and other members of a class cannot be exposed as part of an XML Web service.

Table 19-3 lists the properties available for use with the WebMethod attribute.

Table 19-3. The WebMethod Properties

Property

Description

BufferResponse

If set to true, the response to an XML Web service request will be buffered until complete and returned to the client all at once; responses are buffered by default.

CacheDuration

Defines the number of seconds that a response from an XML Web service method is cached in memory; the default is 0, meaning that responses are not cached. During the cache period, requests for the Web method that pass the same parameter values will return the cached response.

Description

A description of the XML Web service method. Defaults to an empty string.

EnableSession

See the State Management section later in this chapter for more information. EnableSession is disabled by default.

MessageName

The name that will be used to expose the method to clients. By default, the C# method name is used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset