You can use Visual Studio to build a web service and in this case you will not go through the process described in the articles How to build basic web service with ASP.NET in VB.NET – step 1 , How to build basic web service with ASP.NET – step 2, and How to build basic web service with ASP.NET in VB.NET- step 3. Instead, you can create the .asmx file the code-behind in one step, by selecting Website->Add New Item from the menu. You can choose to put the web service code directly in the .asmx file or in a separate code-behind file, just as you can with a web page. In this case you do not see two other web service details:
– The web service class inherits from System.Web.Services.WebService
– A WebService attribute is applied to the class declaration.
When you create a web service in Visual Studio, your web service class automatically derives from the base WebService class, as shown here:
Public Class BooksService
Inherits System.Web.Services.WebService
End Class
In this case you can access the built-in ASP.NET objects (such as Application, Session, and User) as easily as you can in a web form. These objects are provided as properties of the WebService class. If you don’t need to use any of these objects, you don’t need to inherit.
The next code lines show how you can access Application state in a web service if you derive from the base WebService class:
‘ Store a number in session state.
Session(“Counter”) = 77
You should use the next code if your web service class doesn’t derive from WebService:
‘ Store a number in session state.
HttpContext.Current.Session(“Counter”) = 77
The next table describes properties you receive by inheriting from WebService:
Property |
Description |
Application |
An instance of the HttpApplicationState class that provides access to the global application state of the web application. |
Context |
An instance of the HttpContext class for the current request. |
Server |
An instance of the HttpServerUtility class. |
Session | An instance of the HttpSessionState class that provides access to the current session state. |
User |
An IPrincipal object that allow you to examine user credentials and roles, if the user has been authenticated. |
Important notes:
1. Since the .NET Framework supports only single inheritance, inheriting from WebService means your web service class cannot inherit from other classes. This is really the only reason not to inherit from WebService.
2. WebService is derived from the System.MarshalByRefObject class which is the base class used for .NET remoting. When you create a class that derives from WebService, you gain the ability to use your class in several ways:
a. You can use it as any other local class (and access it directly in your web pages),
b. You can expose it as part of a web service
c. You can expose it as a distributed object in a .NET remote host.