[英]Caching in ASP.net
我有一個webService。 因此,我正在使用緩存。
我編寫了以下代碼以將數據表存儲在緩存中。
using System.Web.Caching;
Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
它給我這樣的錯誤,例如“非靜態字段需要對象引用。
我如何清除此錯誤
您正在嘗試將Cache類用作靜態類。
如果要在請求期間將當前Cache類實例用於HTTP上下文,則應執行以下操作:
HttpContext.Current.Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
使用System.Web.HttpRuntime.Cache .Insert(...)。 此“緩存”是一個屬性,返回System.Web.Caching.Cache的實例 。 它與HttpContext.Current.Cache相同,但是不需要HttpContext。
您的代碼試圖在不使用實例的情況下訪問該類的方法-因此出現錯誤消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.