Web Service Class and Directive in ASP.NET

Introduction

 
In this article, I am going to look beyond just VS.NET, at the options we have for configuring a Web Service using the Web Service class and directives that are available with ASP.NET.
 

WebService Class

 
A Web Service class may inherit from a class in the System.Web.Services namespace, the WebService class. The code emitted by Visual Studio .NET inherits from this class. see here
 
Creating a Simple Web Service
 
Use the following steps to create a web service.
  • Go to Visual Studio 2010 and create a New Project.
img1.gif
  • Select .NET Framework 3.5.
  • Create an ASP.NET Web Service Application.
  • Give it a name and click the ok button.
img2.gif
 
We will see the following code.
  1. public class Service1 : System.Web.Services.WebService  
  2. {  
  3.         [WebMethod]  
  4.         public string HelloWorld()  
  5.         {  
  6.            return "Hello World";  
  7.         }  
  8. }  
Inheriting from the WebService class is mainly convenient.
  • It does not add any functionality to our Web Service.
  • The convenience is that the WebService class provides direct access to the Web.HTTPContext object of the current request.
  • By inheriting from the WebService class we can access the Application object directly.
  • The .NET Framework only supports single inheritance; inheriting from WebService means that our class cannot inherit from other classes. This is really the only reason not to inherit from WebService.
  • An interesting point with inheriting from WebService is that WebService is derived from the System.MarshalByRefObject class. This class is the base class for .NET Remoting.
The WebService class has the following properties:
  • Application- The Application object holding the application state of the Web Service.
  • Context- Gets the ASP.NET HttpContext for the current request, which encapsulates all HTTP-specific context used by the HTTP server to process Web requests.
  • Server- The HTTPServerUtility object of the current request. This is similar to the Server object found in ASP. It contains, for instance, the CreateObject() function.
  • Session- The Session object holding the session state of the Web Service.
  • User- The User object is used to get access to security information such as whether the user is authenticated and the name of the authenticated user.

WebService Directive

 
When we create an XML Web service in ASP. NET, we place the required @ WebService directive at the top of a text file with a .asmx file name extension. The presence of the .asmx file and the @ WebService directive correlates the URL address of the XML Web service with its implementation.
 
The @ WebService directive tells the ASP.NET runtime that the .asmx file contains an XML Web service and provides information about the implementation of the Web service. Here is the example of an @ WebService directive:
  1. <%@ WebService Language="C#" CodeBehind="Akshay.asmx.cs" Class="Akshay" %>  
The directive is required for ASP.NET Web Services. If the class for the Web Service is in a code-behind file, this line will be the only line in the .asmx file.
 
The directive may use the following attributes.
  • Language
  • Codebehind
  • Class
  • Debug
Language
 
The Language attribute is optional. It specifies the language to use to compile the Web Service. Any .NET compiler installed on the system may be specified. By default, the installed compilers are C#, VB.NET, and JScript.NET. These are specified using the values VB, C#, or JS.
  1. <%@ WebService Language="C#" %> 
The default language is VB.NET as specified in the machine.config file, unless it is overridden in the web.config file.
 
Codebehind
 
The Codebehind attribute is optional. It is used by Visual Studio .NET to find the code-behind file for the .asmx file so that when you click on the .asmx file it can open the code-behind file. The attribute is only used in Visual Studio .NET; it has no effect when the Web Service is executing.
  1. <%@ WebService Language="C#" CodeBehind="Akshay.asmx.cs" Class="Akshay" %>  
Class
 
The Class attribute specifies the class to expose as a Web Service. If the class is within a namespace, the namespace is specified, as in the following example. In this example, the namespace is MyVSDemo and the class is on Akshay.
  1. <%@ WebService class="MyVSDemo.Akshay"%>  
 If the class is not within a namespace, we specify the class directly:
  1. <%@ WebService class="Akshay"%>  
 The class attribute may optionally also specify the assembly where the class exists:
  1. <%@ WebService class="MyVSDemo.Akshay, MyVSDemo"%>  
If the assembly is not specified, ASP.NET searches all assemblies in the bin directory for the Akshay class.
 
Debug
 
Indicates whether the XML Web service should be compiled with debug symbols; true if the XML Web service should be compiled with debug symbols; otherwise, false.
  1. <%@ WebService Language="C#" CodeBehind="Akshay.asmx.cs" Class="Akshay"  debug="true"%>  
Resources