This documentation is archived and is not being maintained.

WebMethodAttribute.CacheDuration Property

Gets or sets the number of seconds the response should be held in the cache.

Namespace: System.Web.Services
Assembly: System.Web.Services (in

public int CacheDuration { get; set; }
/** @property */
public int get_CacheDuration ()

/** @property */
public void set_CacheDuration (int value)

public function get CacheDuration () : int

public function set CacheDuration (value : int)

Not applicable.

Property Value

The number of seconds the response should be held in the cache. The default is 0, which means the response is not cached.

When caching is enabled requests and responses are held in memory on the server for at least the cache duration so caution must be used if you expect requests or responses to be very large or you expect requests to vary widely.

There are two issues that can affect output caching in an ASP.NET 2.0 Web service application.

In ASP.NET 2.0 the HTTP method of the test page has changed from GET to POST. However, POSTs are not normally cached. If you change the test page in an ASP.NET 2.0 Web service application to use GET, caching works properly.

In addition, HTTP indicates that a user agent (the browser or calling application) should be able to override server caching by setting the "Cache-Control" to "no-cache". ASP.NET applications, therefore, ignore cached results when they find a "no-cache" header.

The following example places the result of the call to the ServiceUsage XML Web service method in the cache for 60 seconds. Any time an XML Web service client executes the ServiceUsage XML Web service method during that time, the same result is returned.

<%@ 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 Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0