如何:在使用 ASP.NET 创建的 Web 服务中管理状态
当实现 Web 服务的类派生自 WebService 类时,Web 服务可与其他 ASP.NET 应用程序访问相同的状态管理选项。 WebService 类包含许多个公共 ASP.NET 对象,包括 Session 和 Application 对象。
访问并存储特定的客户端会话所特有的状态
声明一项 Web 服务。
<%@ WebService Language="C#" Class="ServerUsage" %>
<%@ WebService Language="VB" Class="ServerUsage" %>
添加对 System.Web.Services 命名空间的引用。
using System.Web.Services;
Imports System.Web.Services
从 WebService 派生实现 Web 服务的类。
public class ServerUsage : WebService
Public Class ServerUsage : Inherits WebService
声明 Web 服务方法,并将 WebMethod 属性 (Attribute) 的 EnableSession 属性 (Property) 设置为 true。
[ WebMethod(EnableSession=true) ] public int PerSessionServiceUsage()
< WebMethod(EnableSession:=True) > _ Public Function PerSessionServiceUsage() As Integer
在 Session 中存储状态,该属性为后面的检索指定状态的名称。 在下面的示例中,值
1
存储在名为MyServiceUsage
的状态变量中。Session["MyServiceUsage"] = 1;
Session("MyServiceUsage") = 1
访问存储在 Session 中的状态变量。
在下面的示例中,将访问
MyServiceUsage
状态变量以增加其值。Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;
Session("MyServiceUsage") = CInt(Session("MyServiceUsage")) + 1
访问并存储承载 Web 服务的 Web 应用程序所特有的状态
声明一项 Web 服务。
<%@ WebService Language="C#" Class="ServerUsage" %>
<%@ WebService Language="VB" Class="ServerUsage" %>
添加对 System.Web.Services 命名空间的引用。
using System.Web.Services;
Imports System.Web.Services
从 WebService 派生实现 Web 服务的类。
public class ServerUsage : WebService
Public Class ServerUsage : Inherits WebService
声明 Web 服务方法。
[ WebMethod ] public int PerSessionServiceUsage()
< WebMethod > _ Public Function PerSessionServiceUsage() As Integer
在 Application 中存储状态,该属性为后面的检索指定状态的名称。 在下面的示例中,值
1
存储在名为appMyServiceUsage
的状态变量中。Application["appMyServiceUsage"] = 1;
Application("appMyServiceUsage") = 1
访问存储在 Application 中的状态变量。
在下面的示例中,将访问
appMyServiceUsage
状态变量以增加其值。Application["appMyServiceUsage"] = ((int) Application["appMyServiceUsage"]) + 1;
Application("appMyServiceUsage") = _ CInt(Application("appMyServiceUsage")) + 1
示例
<%@ 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
请参见
其他资源
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。