WebMethodAttribute.CacheDuration Proprietà

Definizione

Ottiene o imposta il numero di secondi in cui deve essere contenuta la risposta nella cache.

public:
 property int CacheDuration { int get(); void set(int value); };
public int CacheDuration { get; set; }
member this.CacheDuration : int with get, set
Public Property CacheDuration As Integer

Valore della proprietà

Numero di secondi in cui la risposta deve essere contenuta nella cache. Il valore predefinito è 0, ovvero la risposta non viene memorizzata nella cache.

Esempio

L'esempio seguente inserisce il risultato della chiamata al metodo del ServiceUsage servizio Web XML nella cache per 60 secondi. Ogni volta che un client del servizio Web XML esegue il metodo del ServiceUsage servizio Web XML durante tale periodo, viene restituito lo stesso risultato.

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

Commenti

Quando la memorizzazione nella cache è abilitata per le richieste e le risposte vengono mantenute in memoria nel server per almeno la durata della cache, è necessario prestare attenzione se si prevede che le richieste o le risposte siano molto grandi o si prevede che le richieste variano notevolmente.

Esistono due problemi che possono influire sulla memorizzazione nella cache dell'output in un'applicazione di servizio Web ASP.NET 2.0.

In ASP.NET 2.0 il metodo HTTP della pagina di test è cambiato da GET a POST. Tuttavia, i file POST non vengono in genere memorizzati nella cache. Se si modifica la pagina di test in un'applicazione di servizio Web ASP.NET 2.0 per usare GET, la memorizzazione nella cache funziona correttamente.

HTTP indica inoltre che un agente utente (il browser o l'applicazione chiamante) deve essere in grado di eseguire l'override della memorizzazione nella cache del server impostando "Cache-Control" su "no-cache". ASP.NET applicazioni, pertanto, ignorare i risultati memorizzati nella cache quando trovano un'intestazione "no-cache".

Si applica a