明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

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

[摘要]在下面示例中,appMyServiceUsage狀態(tài)變量被訪問來遞增其值。下面的代碼示例是一個使用兩個XML Web服務(wù)方法的XML Web服務(wù):ServerUsage和PerSessionServerUage。ServerUsage是一個點擊計數(shù)器,用于訪問ServerUsage XML Web...

    

  在下面示例中,appMyServiceUsage狀態(tài)變量被訪問來遞增其值。下面的代碼示例是一個使用兩個XML Web服務(wù)方法的XML Web服務(wù):ServerUsage和PerSessionServerUage。ServerUsage是一個點擊計數(shù)器,用于訪問ServerUsage XML Web服務(wù)方法時計數(shù),而不管客戶端如何與XML Web服務(wù)方法通信。例如,如果三個客戶端連續(xù)地調(diào)用ServerUsage XML Web服務(wù)方法,最后一個接收一個返回值3。而PerSessionServiceUsage則是用于一個特別的客戶端會話的計數(shù)器。如果三個客戶端連續(xù)地訪問PerSessionServiceUsage,每個客戶端都會在第一次調(diào)用的時候接收到相同的結(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