明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

ASP.NET創(chuàng)建XML Web服務(wù)全接觸(12)

[摘要]在下面示例中,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