Export (0) Print
Expand All

WebMethodAttribute.CacheDuration Property

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

[Visual Basic]
Public Property CacheDuration As Integer
[C#]
public int CacheDuration {get; set;}
[C++]
public: __property int get_CacheDuration();
public: __property void set_CacheDuration(int);
[JScript]
public function get CacheDuration() : int;
public function set CacheDuration(int);

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.

Remarks

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.

Example

[Visual Basic, C#] 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.

[Visual Basic] 
<%@ 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


[C#] 
<%@ 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"];
     }
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

WebMethodAttribute Class | WebMethodAttribute Members | System.Web.Services Namespace

Show:
© 2014 Microsoft