·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » ASP.NET创建XML Web服务全接触(12)

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

类别: ASP.NET教程  评论数:0 总得分:0
管理Web服务状态(2)

    

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

-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1