WebMethodAttribute.CacheDuration Properti

Definisi

Mendapatkan atau mengatur jumlah detik respons harus ditahan di 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

Nilai Properti

Jumlah detik respons harus ditahan di cache. Defaultnya adalah 0, yang berarti respons tidak di-cache.

Contoh

Contoh berikut menempatkan hasil panggilan ke ServiceUsage metode layanan Web XML di cache selama 60 detik. Setiap kali klien layanan Web XML menjalankan ServiceUsage metode layanan Web XML selama waktu tersebut, hasil yang sama dikembalikan.

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

Keterangan

Ketika penembolokan diaktifkan permintaan dan respons disimpan dalam memori di server untuk setidaknya durasi cache sehingga perhatian harus digunakan jika Anda mengharapkan permintaan atau respons menjadi sangat besar atau Anda mengharapkan permintaan bervariasi secara luas.

Ada dua masalah yang dapat memengaruhi penembolokan output dalam aplikasi layanan Web ASP.NET 2.0.

Di ASP.NET 2.0 metode HTTP halaman pengujian telah berubah dari GET ke POST. Namun, POS Biasanya tidak di-cache. Jika Anda mengubah halaman pengujian di aplikasi layanan Web ASP.NET 2.0 untuk menggunakan GET, penembolokan berfungsi dengan baik.

Selain itu, HTTP menunjukkan bahwa agen pengguna (browser atau aplikasi panggilan) harus dapat mengambil alih penembolokan server dengan mengatur "Cache-Control" ke "no-cache". ASP.NET aplikasi, oleh karena itu, abaikan hasil yang di-cache saat menemukan header "tanpa cache".

Berlaku untuk