工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛
您的位置:软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > ASP.net > 正文
ASP.NET创建Web服务之管理Web服务状态
[文章信息]
作者:WAYNE编译
时间:2004-11-22
出处:天极网
责任编辑:方舟
[文章导读]
XML Web服务在类实现派生于WebService类的XML Web服务时,可以使用和其他ASP.NET应用程序相同的状态管理选项
advertisement
热点推荐
· ImageReady制作“焰火”小动画
· Java加密和数字签名编程快速入门
· 在VB6中用命令行为模式控制GUI动作
· Excel图表向导详解
· 浅析各种计算机病毒应对方法
[正文]

上一页  1 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

上一页  1 2  

发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容焦点新闻
  • 英特尔澄清杨旭任职传闻 官方没宣布此消息
  • 国资委河北密制联通拆分方案
  • 垃圾邮件害人害企害国 清除垃圾邮件不手软
  • 中兴携手阿尔卡特 全球逐鹿CDMA
  • 用友总裁王文京:誓将ERP变成“大众消费”
  • 香港消费者委员会:数码相机最贵未必最好
  • 外电称中兴正评估西门子手机业务 或能并购
  • 国信办督战八大行业灾难备份 将出台国家标准
  • Advertisement