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
Declare a Web service.
<%@ WebService Language="C#" Class="ServerUsage" %>
<%@ WebService Language="VB" Class="ServerUsage" %>
Add a reference to the System.Web.Services namespace.
using System.Web.Services;
Imports System.Web.Services
Derive the class that implements the Web service from WebService .
public class ServerUsage : WebService
Public Class ServerUsage : Inherits WebService
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
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 namedMyServiceUsage
.Session["MyServiceUsage"] = 1;
Session("MyServiceUsage") = 1
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
Declare a Web service.
<%@ WebService Language="C#" Class="ServerUsage" %>
<%@ WebService Language="VB" Class="ServerUsage" %>
Add a reference to the System.Web.Services namespace.
using System.Web.Services;
Imports System.Web.Services
Derive the class that implements the Web service from WebService .
public class ServerUsage : WebService
Public Class ServerUsage : Inherits WebService
Declare a Web service method.
[ WebMethod ] public int PerSessionServiceUsage()
< WebMethod > _ Public Function PerSessionServiceUsage() As Integer
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 namedappMyServiceUsage
.Application["appMyServiceUsage"] = 1;
Application("appMyServiceUsage") = 1
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