How to: Manage State in Web Services Created Using ASP.NET

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation.

Web services have access to the same state management options as other ASP.NET applications when the class that implements the Web service derives from the WebService class. The WebService class contains many of the common ASP.NET objects, including the Session and Application objects.

To access and store state specific to a particular client session

  1. Declare a Web service.

    <%@ WebService Language="C#" Class="ServerUsage" %>
    
    <%@ WebService Language="VB" Class="ServerUsage" %>
    
  2. Add a reference to the System.Web.Services namespace.

    using System.Web.Services;
    
    Imports System.Web.Services
    
  3. Derive the class that implements the Web service from WebService .

    public class ServerUsage : WebService 
    
    Public Class ServerUsage : Inherits WebService
    
  4. Declare a Web service method, setting the EnableSession property of the WebMethod attribute to true.

    [ WebMethod(EnableSession=true) ]
    public int PerSessionServiceUsage()
    
    < WebMethod(EnableSession:=True) > _
    Public Function PerSessionServiceUsage() As Integer
    
  5. Store state in the Session, which specifies a name for the state for later retrieval. In the following example the value 1 is stored in a state variable named MyServiceUsage.

    Session["MyServiceUsage"] = 1;
    
    Session("MyServiceUsage") = 1
    
  6. Access the state variable stored in the Session .

    In the following example, the MyServiceUsage state variable is accessed to increment its value.

    Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;
    
    Session("MyServiceUsage") = CInt(Session("MyServiceUsage")) + 1
    

To access and store state specific to the Web application hosting the Web service

  1. Declare a Web service.

    <%@ WebService Language="C#" Class="ServerUsage" %>
    
    <%@ WebService Language="VB" Class="ServerUsage" %>
    
  2. Add a reference to the System.Web.Services namespace.

    using System.Web.Services;
    
    Imports System.Web.Services
    
  3. Derive the class that implements the Web service from WebService .

    public class ServerUsage : WebService
    
    Public Class ServerUsage : Inherits WebService
    
  4. Declare a Web service method.

    [ WebMethod ]
    public int PerSessionServiceUsage()
    
    < WebMethod > _
    Public Function PerSessionServiceUsage() As Integer
    
  5. Store state in the Application, which specifies a name for the state for later retrieval. In the following example the value 1 is stored in a state variable named appMyServiceUsage.

    Application["appMyServiceUsage"] = 1;
    
    Application("appMyServiceUsage") = 1
    
  6. Access the state variable stored in the Application.

    In the following example, the appMyServiceUsage state variable is accessed to increment its value.

    Application["appMyServiceUsage"] =
       ((int) Application["appMyServiceUsage"]) + 1;
    
    Application("appMyServiceUsage") = _
       CInt(Application("appMyServiceUsage")) + 1
    

Example

<%@ WebService Language="C#" Class="ServerUsage" %>
using System.Web.Services;

public class ServerUsage : WebService {
   [ WebMethod(Description="Number of times this service has been accessed.") ]
   public int ServiceUsage() {
     // If the Web service method hasn't been accessed,
     // initialize it to 1.
     if (Application["appMyServiceUsage"] == null) 
     {
       Application["appMyServiceUsage"] = 1;
     }
     else
     {
     // Increment the usage count.
       Application["appMyServiceUsage"] = ((int) Application["appMyServiceUsage"]) + 1;
     }
     return  (int) Application["appMyServiceUsage"];
   }

   [ WebMethod(Description="Number of times a particular client session has accessed this Web service method.",EnableSession=true) ]
   public int PerSessionServiceUsage() {
     // If the Web service method hasn't been accessed, initialize
     // it to 1.
     if (Session["MyServiceUsage"] == null) 
     {
       Session["MyServiceUsage"] = 1;
     }
     else
     {
     // Increment the usage count.
       Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;
     }
     return  (int) Session["MyServiceUsage"];
   }
}
<%@ WebService Language="VB" Class="ServerUsage" %>
Imports System.Web.Services

Public Class ServerUsage
    Inherits WebService
    
<WebMethod(Description := "Number of times this service has been accessed.")> _
    Public Function ServiceUsage() As Integer
        ' If the Web service method hasn't been accessed, initialize
        ' it to 1.
        If Application("appMyServiceUsage") Is Nothing Then
            Application("appMyServiceUsage") = 1
        Else
            ' Increment the usage count.
            Application("appMyServiceUsage") = _
               CInt(Application("appMyServiceUsage")) + 1
        End If
        Return CInt(Application("appMyServiceUsage"))
    End Function    
    
<WebMethod(Description := "Number of times a particular client session has accessed this Web service method.", EnableSession := True)> _
    Public Function  PerSessionServiceUsage() As Integer
       ' If the Web service method hasn't been accessed,
       ' initialize it to 1.
        If Session("MyServiceUsage") Is Nothing Then
            Session("MyServiceUsage") = 1
        Else
            ' Increment the usage count.
           Session("MyServiceUsage") = CInt(Session("MyServiceUsage")) + 1
        End If
        Return CInt(Session("MyServiceUsage"))
    End Function
    
End Class

See Also

Other Resources

ASP.NET State Management