Web Service Attributes

Two primary attributes for building a .NET Web service are WebService and WebMethod. The optional WebService attribute is applied to the class containing methods called by client applications. Each method that is exposed to callers must be decorated with the WebMethod attribute.

The optional WebService attribute decorates the Web Service class, providing a meaningful name, description, and namespace. Table 17.2 describes the parameters available to the WebService attribute.

Table 17.2. WebService Attribute Parameters

PARAMETER

DESCRIPTION

Description

Explains the purpose of the Web service.

Name

Name of the Web service.

Namespace

Namespace to distinguish this Web service from others.

The Description parameter is simply documentation, but the Name and Namespace parameters are important to the operation of the Web service. The default value of the Web Service name is the class identifier. The Name parameter changes this so that the Web service can be identified by a different name. A Namespace helps identify and distinguish this Web service from others. The default Namespace value is http://www.tempuri.org, but this must be changed before deployment as it will cause problems with properly identifying the Web service. Although the Namespace specified may look like a URL, this is just a common convention and the site may or may not exist. The only requirement is that the namespace be unique for identification purposes. Listing 17.3 shows a sample WebService attribute.

Listing 17.3 The WebService Attribute
 [WebService(    Name="ProductsTableService",    Namespace="http://www.samspublishing.com/",    Description="Performs Products table commands.")] public class DataService: System.Web.Services.WebService {    // class implementation removed } 

The effects of applying the WebService attribute in Listing 17.3 are that the Web Service helper page no longer gives warnings about using the default namespace. Also, the Name and description appear as specified in the WebService attribute. The differences can be seen by running the Web service in the helper page with the WebService attribute both commented out and uncommented.

As seen in earlier listings in this chapter, Web Service methods are decorated with the WebMethod attribute. Although just adding this attribute enables a method to be called via Web Service protocols, it also has parameters that help document and customize the behavior of the method. Table 17.3 describes those parameters.

Table 17.3. WebMethod Attribute Parameters

PARAMETER

DESCRIPTION

BufferResponse

Buffers response when set to true.

CacheDuration

Caches results for a specified number of seconds to improve performance.

Description

Describes what the Web method does.

EnableSession

By default, the ability to manage session state is turned off. Setting this to true enables it. Session is enabled by method only, and may not be set for an entire Web service.

MessageName

Web services don't support method name overloading, but C# does. To allow the class to overload a method and still expose it as a Web method, this parameter must be set so that each overloaded method has a different name, as seen by clients.

TransactionOption

Sets transaction options to indicate whether transactions are disabled, supported, or required.

Listing 17.4 shows a WebMethod attribute with a Description parameter. Other parameters are added as a comma-separated list as required.

Listing 17.4 The WebMethod Attribute
 [WebMethod(Description="Returns data from Product table.")] public DataSet GetProducts() {    return productsDS; } 


C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net