明辉站/网站教程/内容

ASP.NET创建XML Web服务全接触(12)

网站教程2024-06-08 阅读
[摘要]在下面示例中,appMyServiceUsage状态变量被访问来递增其值。下面的代码示例是一个使用两个XML Web服务方法的XML Web服务:ServerUsage和PerSessionServerUage。ServerUsage是一个点击计数器,用于访问ServerUsage XML Web...

    

  在下面示例中,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


……

相关阅读