Deriving from the WebService Class

In the StockTickerSimple web service, with the class file listed in Example 15-4, the Service class inherits from the WebService class.

Deriving from the WebService class is optional, but it offers several advantages. The principal one is that you gain access to several common ASP.NET objects:

Application and Session

These objects allow the application to take advantage of state management. For a complete discussion of state management, see Chapter 6. State as it pertains specifically to web services will be covered in more detail later in this chapter.

User

This object is useful for authenticating the caller of a web service. For a complete discussion of security, see Chapter 12.

Context

This object provides access to all HTTP-specific information about the caller’s request contained in the HttpContext class.

The main reason you might not want to inherit from WebService is to overcome the limitation imposed by the .NET Framework that a class can only inherit from one other class. Multiple inheritance is not supported. It would be very inconvenient to have to inherit from WebService if you needed to inherit from another class.

Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.