ASP.NET創建Web服務之管理Web服務狀態
XML Web服務在類實現派生于WebService類的XML Web服務時,可以使用和其他ASP.NET應用程序相同的狀態管理選項。WebService類包含許多公共ASP.NET對象,包括Session和Application對象。
Application對象提供一個儲存運行在Web應用程序中的代碼可訪問的數據的機制,而Session對象允許數據存儲在每客戶端會話基礎上。如果客戶端支持cookies,那么可以使用cookie來識別客戶端會話。
Session對象中存儲的數據只有在WebMethod屬性的EnableSession屬性設置為true來使用WebService派生的類時才可用。WebService派生的類自動可訪問Application對象。
訪問和保存特定客戶端會話的特定狀態
聲明一個XML Web服務。
添加一個引用到System.Web.Services域名空間。
從WebService類派生實現XML Web服務的類。
聲明一個XML Web服務方法,設置WebMethod屬性的EnableSession屬性為true。
保存狀態在會話中,為狀態指定一個名稱,便于稍后的檢索。 在下面的示例中,值1被保存在一個名為MyServiceUsage的狀態變量中。
訪問保存在Session中的狀態變量。
在下面示例中,MyServiceUsage狀態變量被訪問來遞增其值。
訪問并保存Web應用程序中的XML Web服務的特定的狀態
聲明一個XML Web服務。
添加一個引用到System.Web.Services域名空間。
從WebService類派生實現XML Web服務的類。
聲明一個XML Web服務方法。
保存狀態在Application中,為狀態指定一個名稱,便于稍后的檢索。在下面的示例中,值1被保存在一個名為appMyServiceUsage的狀態變量中。
訪問保存在Application中的狀態變量。
在下面示例中,appMyServiceUsage狀態變量被訪問來遞增其值。
在下面示例中,appMyServiceUsage狀態變量被訪問來遞增其值。下面的代碼示例是一個使用兩個XML Web服務方法的XML Web服務:ServerUsage和PerSessionServerUage。ServerUsage是一個點擊計數器,用于訪問ServerUsage XML Web服務方法時計數,而不管客戶端如何與XML Web服務方法通信。例如,如果三個客戶端連續地調用ServerUsage XML Web服務方法,最后一個接收一個返回值3。而PerSessionServiceUsage則是用于一個特別的客戶端會話的計數器。如果三個客戶端連續地訪問PerSessionServiceUsage,每個客戶端都會在第一次調用的時候接收到相同的結果。
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" %> |
添加一個引用到System.Web.Services域名空間。
[C#] using System.Web.Services; [Visual Basic] Imports System.Web.Services |
從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 |
保存狀態在會話中,為狀態指定一個名稱,便于稍后的檢索。 在下面的示例中,值1被保存在一個名為MyServiceUsage的狀態變量中。
[C#] Session["MyServiceUsage"] = 1; [Visual Basic] Session("MyServiceUsage") = 1 |
訪問保存在Session中的狀態變量。
在下面示例中,MyServiceUsage狀態變量被訪問來遞增其值。
[C#] Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1; [Visual Basic] Session("MyServiceUsage") = CInt(Session("MyServiceUsage")) + 1 |
訪問并保存Web應用程序中的XML Web服務的特定的狀態
聲明一個XML Web服務。
[C#] <%@ WebService Language="C#" Class="ServerUsage" %> [Visual Basic] <%@ WebService Language="VB" Class="ServerUsage" %> |
添加一個引用到System.Web.Services域名空間。
[C#] using System.Web.Services; [Visual Basic] Imports System.Web.Services |
從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中,為狀態指定一個名稱,便于稍后的檢索。在下面的示例中,值1被保存在一個名為appMyServiceUsage的狀態變量中。
[C#] Application["appMyServiceUsage"] = 1; [Visual Basic] Application("appMyServiceUsage") = 1 |
訪問保存在Application中的狀態變量。
在下面示例中,appMyServiceUsage狀態變量被訪問來遞增其值。
[C#] Application["appMyServiceUsage"] = ((int) Application["appMyServiceUsage"]) + 1; [Visual Basic] Application("appMyServiceUsage") = _ CInt(Application("appMyServiceUsage")) + 1 |
在下面示例中,appMyServiceUsage狀態變量被訪問來遞增其值。下面的代碼示例是一個使用兩個XML Web服務方法的XML Web服務:ServerUsage和PerSessionServerUage。ServerUsage是一個點擊計數器,用于訪問ServerUsage XML Web服務方法時計數,而不管客戶端如何與XML Web服務方法通信。例如,如果三個客戶端連續地調用ServerUsage XML Web服務方法,最后一個接收一個返回值3。而PerSessionServiceUsage則是用于一個特別的客戶端會話的計數器。如果三個客戶端連續地訪問PerSessionServiceUsage,每個客戶端都會在第一次調用的時候接收到相同的結果。
[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 XML 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 particualr client session has accessed this XML Web service method.",EnableSession=true) ] public int PerSessionServiceUsage() { // If the XML 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"]; } } [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 XML 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 XML Web service method.", EnableSession := True)> _ Public Function PerSessionServiceUsage() As Integer ' If the XML 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 |