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.

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

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

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

    C#
    [ WebMethod(EnableSession=true) ]
    public int PerSessionServiceUsage()
    
  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.

    C#
    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.

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

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

  1. Declare a Web service.

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

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

    C#
    public class ServerUsage : WebService
    
  4. Declare a Web service method.

    C#
    [ WebMethod ]
    public int PerSessionServiceUsage()
    
  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.

    C#
    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.

    C#
    Application["appMyServiceUsage"] =
       ((int) Application["appMyServiceUsage"]) + 1;
    

Example

C#
<%@ 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"];
   }
}

See Also

Other Resources

ASP.NET State Management