WebMethodAttribute.CacheDuration Proprietà

Definizione

Ottiene o imposta il numero di secondi che indicano la durata del mantenimento della 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à

Int32

Numero di secondi che indicano la durata del mantenimento della risposta nella cache. Il valore predefinito è 0, che indica che 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 ServiceUsage metodo del 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, le richieste e le risposte vengono mantenute in memoria nel server per almeno la durata della cache, pertanto è necessario prestare attenzione se si prevede che le richieste o le risposte siano molto grandi o si prevede che le richieste variano notevolmente.

Vi sono due fattori che possono influire sulla memorizzazione dell'output nella cache in un'applicazione servizio Web ASP.NET 2.0.

In ASP.NET 2.0 il metodo HTTP della pagina di prova non è più GET, ma POST. I metodi POST, tuttavia, in genere non vengono memorizzati nella cache. Se si modifica la pagina di prova in un'applicazione servizio Web ASP.NET 2.0 in modo che venga utilizzato GET, la memorizzazione nella cache non presenta problemi.

Inoltre per HTTP è indicato che un agente utente (il browser o l'applicazione chiamante) dovrebbe essere in grado di eseguire l'override della memorizzazione nella cache impostando "Cache-Control" su "no-cache". Pertanto le applicazioni ASP.NET ignorano i risultati memorizzati nella cache quando trovano un'intestazione "no-cache".

Si applica a