ASP.NET創(chuàng)建XML Web服務(wù)全接觸(12)
發(fā)表時(shí)間:2024-02-07 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在下面示例中,appMyServiceUsage狀態(tài)變量被訪問(wèn)來(lái)遞增其值。下面的代碼示例是一個(gè)使用兩個(gè)XML Web服務(wù)方法的XML Web服務(wù):ServerUsage和PerSessionServerUage。ServerUsage是一個(gè)點(diǎn)擊計(jì)數(shù)器,用于訪問(wèn)ServerUsage XML Web...
在下面示例中,appMyServiceUsage狀態(tài)變量被訪問(wèn)來(lái)遞增其值。下面的代碼示例是一個(gè)使用兩個(gè)XML Web服務(wù)方法的XML Web服務(wù):ServerUsage和PerSessionServerUage。ServerUsage是一個(gè)點(diǎn)擊計(jì)數(shù)器,用于訪問(wèn)ServerUsage XML Web服務(wù)方法時(shí)計(jì)數(shù),而不管客戶端如何與XML Web服務(wù)方法通信。例如,如果三個(gè)客戶端連續(xù)地調(diào)用ServerUsage XML Web服務(wù)方法,最后一個(gè)接收一個(gè)返回值3。而PerSessionServiceUsage則是用于一個(gè)特別的客戶端會(huì)話的計(jì)數(shù)器。如果三個(gè)客戶端連續(xù)地訪問(wèn)PerSessionServiceUsage,每個(gè)客戶端都會(huì)在第一次調(diào)用的時(shí)候接收到相同的結(jié)果。[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 |