快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

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

治理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


返回类别: 教程
上一教程: .NET和SQL Server中“空值”辨析
下一教程: .NET中Form.AutoScrollPosition的运用

您可以阅读与"ASP.NET创建XML Web服务全接触(12)"相关的教程:
· ASP.NET创建XML Web服务全接触(13)
· ASP.NET创建XML Web服务全接触(14)
· ASP.NET创建XML Web服务全接触(2)
· ASP.NET创建XML Web服务全接触(7)
· ASP.NET创建XML Web服务全接触(10)
    微笑服务 优质保证 索取样品