WebMethodAttribute.CacheDuration Propriedade

Definição

Obtém ou define o número de segundos que a resposta deve ser mantida no 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

Valor da propriedade

Int32

O número de segundos que a resposta deve ser mantida no cache. O padrão é 0, o que significa que a resposta não é armazenada em cache.

Exemplos

O exemplo a seguir coloca o resultado da chamada para o ServiceUsage método de serviço Web XML no cache por 60 segundos. Sempre que um cliente de serviço Web XML executa o ServiceUsage método de serviço Web XML durante esse tempo, o mesmo resultado é retornado.

<%@ 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

Comentários

Quando o cache está habilitado, as solicitações e as respostas são mantidas na memória no servidor pelo menos durante a duração do cache, portanto, a cautela deve ser usada se você espera que as solicitações ou respostas sejam muito grandes ou se você espera que as solicitações variem amplamente.

Há dois problemas que podem afetar o cache de saída em um aplicativo de serviço Web ASP.NET 2.0.

No ASP.NET 2.0, o método HTTP da página de teste foi alterado de GET para POST. No entanto, os POSTs normalmente não são armazenados em cache. Se você alterar a página de teste em um aplicativo de serviço Web ASP.NET 2.0 para usar GET, o cache funcionará corretamente.

Além disso, HTTP indica que um agente de usuário (o navegador ou aplicativo de chamada) deve ser capaz de substituir o cache do servidor definindo o "Cache-Control" como "sem cache". ASP.NET aplicativos, portanto, ignoram os resultados armazenados em cache quando encontram um cabeçalho "sem cache".

Aplica-se a