简体   繁体   English

在ASP.net中缓存

[英]Caching in ASP.net

I m having a webService. 我有一个webService。 In that I m using Caching. 因此,我正在使用缓存。

I have wrote following code to store datatable in cache. 我编写了以下代码以将数据表存储在缓存中。

using System.Web.Caching;

Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);

It give me error like "An object Reference is required for non static field. 它给我这样的错误,例如“非静态字段需要对象引用。

How can i remove this error 我如何清除此错误

You're trying to use Cache class as an static one. 您正在尝试将Cache类用作静态类。

If you want to use current Cache class instance for an HTTP context during a request, you should be doing something like: 如果要在请求期间将当前Cache类实例用于HTTP上下文,则应执行以下操作:

HttpContext.Current.Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);

Use System.Web.HttpRuntime.Cache .Insert(...). 使用System.Web.HttpRuntime.Cache .Insert(...)。 This 'Cache' is a property returning an instance of System.Web.Caching.Cache. 此“缓存”是一个属性,返回System.Web.Caching.Cache的实例 It's the same as HttpContext.Current.Cache, but without requiring an HttpContext. 它与HttpContext.Current.Cache相同,但是不需要HttpContext。

Your code was trying to access a method of that class, without using an instance - thus the error message. 您的代码试图在不使用实例的情况下访问该类的方法-因此出现错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM