Click to Rate and Give Feedback
MSDN
MSDN Library

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Developer's Guide
How to: Manage State in Web Services Created Using ASP.NET
[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

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" %>
    

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

    C#
    using System.Web.Services;
    

    Visual Basic
    Imports System.Web.Services
    
  3. Derive the class that implements the Web service from WebService .

    C#
    public class ServerUsage : WebService 
    

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

    C#
    [ WebMethod(EnableSession=true) ]
    public int PerSessionServiceUsage()
    

    Visual Basic
    < 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.

    C#
    Session["MyServiceUsage"] = 1;
    

    Visual Basic
    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;
    

    Visual Basic
    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.

    C#
    <%@ WebService Language="C#" Class="ServerUsage" %>
    

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

    C#
    using System.Web.Services;
    

    Visual Basic
    Imports System.Web.Services
    
  3. Derive the class that implements the Web service from WebService .

    C#
    public class ServerUsage : WebService
    

    Visual Basic
    Public Class ServerUsage : Inherits WebService
    
  4. Declare a Web service method.

    C#
    [ WebMethod ]
    public int PerSessionServiceUsage()
    

    Visual Basic
    < 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.

    C#
    Application["appMyServiceUsage"] = 1;
    

    Visual Basic
    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;
    

    Visual Basic
    Application("appMyServiceUsage") = _
       CInt(Application("appMyServiceUsage")) + 1
    
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.
<b>     if (Application["appMyServiceUsage"] == null) </b>
     {
       <br /><b>Application["appMyServiceUsage"] = 1</b>;
     }
     else
     {
     // Increment the usage count.
       <br /><b>Application["appMyServiceUsage"] = ((int) Application["appMyServiceUsage"]) + 1;</b>
     }
     return  (int) <br /><b>Application["appMyServiceUsage"]</b>;
   }

   [ <br /><b>WebMethod</b>(Description="Number of times a particular client session has accessed this Web service method.",<br /><b>EnableSession=true</b>) ]
   public int PerSessionServiceUsage() {
     // If the Web service method hasn't been accessed, initialize
     // it to 1.
     <br /><b>if (Session["MyServiceUsage"] == null)</b> 
     {
       <br /><b>Session["MyServiceUsage"] = 1;</b>
     }
     else
     {
     // Increment the usage count.
<br /><b>       Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;</b>
     }
     return  (int) <br /><b>Session["MyServiceUsage"];</b>
   }
}
Visual Basic
<%@ 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
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker