XML Web服务在类实现派生于WebService类的XML Web服务时可以使用和其他ASPNET应用程序相同的状态管理选项WebService类包含许多公共ASPNET对象包括Session和Application对象
Application对象提供一个储存运行在Web应用程序中的代码可访问的数据的机制而Session对象允许数据存储在每客户端会话基础上如果客户端支持cookies那么可以使用cookie来识别客户端会话
Session对象中存储的数据只有在WebMethod属性的EnableSession属性设置为true来使用WebService派生的类时才可用WebService派生的类自动可访问Application对象
访问和保存特定客户端会话的特定状态
声明一个XML Web服务
[C#]
<%@ WebService Language=C# Class=ServerUsage %>
[Visual Basic]
<%@ WebService Language=VB Class=ServerUsage %>
添加一个引用到SystemWebServices域名空间
[C#]
using SystemWebServices;
[Visual Basic]
Imports SystemWebServices
从WebService类派生实现XML Web服务的类
[C#]
public class ServerUsage : WebService
[Visual Basic]
Public Class ServerUsage : Inherits WebService
声明一个XML Web服务方法设置WebMethod属性的EnableSession属性为true
[C#]
[ WebMethod(EnableSession=true) ]
public int PerSessionServiceUsage()
[Visual Basic]
< WebMethod(EnableSession:=True) > _
Public Function PerSessionServiceUsage() As Integer
保存状态在会话中为状态指定一个名称便于稍后的检索 在下面的示例中值被保存在一个名为MyServiceUsage的状态变量中
[C#]
Session[MyServiceUsage] = ;
[Visual Basic]
Session(MyServiceUsage) =
访问保存在Session中的状态变量
在下面示例中MyServiceUsage状态变量被访问来递增其值
[C#]
Session[MyServiceUsage] = ((int) Session[MyServiceUsage]) + ;
[Visual Basic]
Session(MyServiceUsage) = CInt(Session(MyServiceUsage)) +
访问并保存Web应用程序中的XML Web服务的特定的状态
声明一个XML Web服务
[C#]
<%@ WebService Language=C# Class=ServerUsage %>
[Visual Basic]
<%@ WebService Language=VB Class=ServerUsage %>
添加一个引用到SystemWebServices域名空间
[C#]
using SystemWebServices;
[Visual Basic]
Imports SystemWebServices
从WebService类派生实现XML Web服务的类
[C#]
public class ServerUsage : WebService
[Visual Basic]
Public Class ServerUsage : Inherits WebService
声明一个XML Web服务方法
[C#]
[ WebMethod ]
public int PerSessionServiceUsage()
[Visual Basic]
< WebMethod > _
Public Function PerSessionServiceUsage() As Integer
保存状态在Application中为状态指定一个名称便于稍后的检索在下面的示例中值被保存在一个名为appMyServiceUsage的状态变量中
[C#]
Application[appMyServiceUsage] = ;
[Visual Basic]
Application(appMyServiceUsage) =
访问保存在Application中的状态变量
在下面示例中appMyServiceUsage状态变量被访问来递增其值
[C#]
Application[appMyServiceUsage] =
((int) Application[appMyServiceUsage]) + ;
[Visual Basic]
Application(appMyServiceUsage) = _
CInt(Application(appMyServiceUsage)) +
在下面示例中appMyServiceUsage状态变量被访问来递增其值下面的代码示例是一个使用两个XML Web服务方法的XML Web服务ServerUsage和PerSessionServerUageServerUsage是一个点击计数器用于访问ServerUsage XML Web服务方法时计数而不管客户端如何与XML Web服务方法通信例如如果三个客户端连续地调用ServerUsage XML Web服务方法最后一个接收一个返回值而PerSessionServiceUsage则是用于一个特别的客户端会话的计数器如果三个客户端连续地访问PerSessionServiceUsage每个客户端都会在第一次调用的时候接收到相同的结果
[C#]
<%@ WebService Language=C# Class=ServerUsage %>
using SystemWebServices;
public class ServerUsage : WebService {
[ WebMethod(Description=Number of times this service has been accessed) ]
public int ServiceUsage() {
// If the XML Web service method hasnt been accessed
// initialize it to
if (Application[appMyServiceUsage] == null)
{
Application[appMyServiceUsage] = ;
}
else
{
// Increment the usage count
Application[appMyServiceUsage] = ((int) Application[appMyServiceUsage]) + ;
}
return (int) Application[appMyServiceUsage];
}
[ WebMethod(Description=Number of times a particualr client session has accessed this XML Web service methodEnableSession=true) ]
public int PerSessionServiceUsage() {
// If the XML Web service method hasnt been accessed initialize
// it to
if (Session[MyServiceUsage] == null)
{
Session[MyServiceUsage] = ;
}
else
{
// Increment the usage count
Session[MyServiceUsage] = ((int) Session[MyServiceUsage]) + ;
}
return (int) Session[MyServiceUsage];
}
}
[Visual Basic]
<%@ WebService Language=VB Class=ServerUsage %>
Imports SystemWebServices
Public Class ServerUsage
Inherits WebService
<WebMethod(Description := Number of times this service has been accessed)> _
Public Function ServiceUsage() As Integer
If the XML Web service method hasnt been accessed initialize
it to
If Application(appMyServiceUsage) Is Nothing Then
Application(appMyServiceUsage) =
Else
Increment the usage count
Application(appMyServiceUsage) = _
CInt(Application(appMyServiceUsage)) +
End If
Return CInt(Application(appMyServiceUsage))
End Function
<WebMethod(Description := Number of times a particular client session has accessed this XML Web service method EnableSession := True)> _
Public Function PerSessionServiceUsage() As Integer
If the XML Web service method hasnt been accessed
initialize it to
If Session(MyServiceUsage) Is Nothing Then
Session(MyServiceUsage) =
Else
Increment the usage count
Session(MyServiceUsage) = CInt(Session(MyServiceUsage)) +
End If
Return CInt(Session(MyServiceUsage))
End Function
End Class