WebMethodAttribute.CacheDuration 属性

获取或设置响应应在缓存中保留的秒数。

**命名空间:**System.Web.Services
**程序集:**System.Web.Services(在 system.web.services.dll 中)

语法

声明
Public Property CacheDuration As Integer
用法
Dim instance As WebMethodAttribute
Dim value As Integer

value = instance.CacheDuration

instance.CacheDuration = value
public int CacheDuration { get; set; }
public:
property int CacheDuration {
    int get ();
    void set (int value);
}
/** @property */
public int get_CacheDuration ()

/** @property */
public void set_CacheDuration (int value)
public function get CacheDuration () : int

public function set CacheDuration (value : int)

属性值

响应应在缓存中保留的秒数。默认值为 0,表示不缓存响应。

备注

如果启用缓存,则请求和响应至少应在缓存期间保留在服务器的内存中;因此,如果预计请求或响应非常大或者预计请求变化很大,则一定要慎用缓存。

有两个问题可以影响 ASP.NET 2.0 Web 服务应用程序中的输出缓存。

在 ASP.NET 2.0 中,测试页的 HTTP 方法已从 GET 更改为 POST。但是 POST 通常不进行缓存。如果在 ASP.NET 2.0 Web 服务应用程序的测试页中改为使用 GET,缓存将正常工作。

此外,HTTP 指示用户代理(浏览器或调用应用程序)应该可以通过将“Cache-Control”设置为“no-cache”以重写服务器缓存。因此,当 ASP.NET 应用程序找到“no-cache”标头时,会忽略缓存的结果。

示例

下面的示例将调用 ServiceUsage XML Web services 方法的结果在缓存中存放 60 秒。只要 XML Web services 客户端在该段时间内执行 ServiceUsage XML Web services 方法,就会返回相同的结果。

<%@ WebService Language="VB" Class="Counter" %>

Imports System.Web.Services
Imports System
Imports System.Web

Public Class Counter
    Inherits WebService  

    <WebMethod(Description := "Number of times this service has been accessed", _
        CacheDuration := 60, _
        MessageName := "ServiceUsage")> _
    Public Function ServiceUsage() As Integer
        
        ' If the XML Web service has not been accessed, initialize it to 1.
        If Application("MyServiceUsage") Is Nothing Then
            Application("MyServiceUsage") = 1
        Else
            ' Increment the usage count.
            Application("MyServiceUsage") = CInt(Application("MyServiceUsage")) + 1
        End If
        
        ' Return the usage count.
        Return CInt(Application("MyServiceUsage"))
    End Function
End Class
<%@ WebService Language="C#" Class="Counter" %>

using System.Web.Services;
using System;
using System.Web;

public class Counter : WebService {
     
     [ WebMethod(Description="Number of times this service has been accessed",
     CacheDuration=60,MessageName="ServiceUsage") ]
     public int ServiceUsage() {
          // If the XML Web service has not been accessed, initialize it to 1.
          if (Application["MyServiceUsage"] == null) {
              Application["MyServiceUsage"] = 1;
          }
          else {
              // Increment the usage count.
              Application["MyServiceUsage"] = ((int) Application["MyServiceUsage"]) + 1;
          }

          // Return the usage count.     
          return  (int) Application["MyServiceUsage"];
     }
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

WebMethodAttribute 类
WebMethodAttribute 成员
System.Web.Services 命名空间