加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 完全无法定义System.Runtime.Caching的UpdateCallback

发布时间:2020-12-15 18:19:38 所属栏目:百科 来源:网络整理
导读:我在使用System.Runtime.Caching库的CacheEntryUpdateCallback委托时遇到了困难.每当我定义和设置回调时,我得到一个ArgumentException,“CacheItemUpdateCallback必须为null”.为什么必须为空?我应该能够设置它然后获得回调. 使用CacheEntryRemovedCallback
我在使用System.Runtime.Caching库的CacheEntryUpdateCallback委托时遇到了困难.每当我定义和设置回调时,我得到一个ArgumentException,“CacheItemUpdateCallback必须为null”.为什么必须为空?我应该能够设置它然后获得回调.

使用CacheEntryRemovedCallback委托时,我不明白这一点.我可以在所有项目中可靠地重现这一点.难道我做错了什么?这是一个小样本应用程序:

using System.Runtime.Caching;
class Program {
  static void Main(string[] args) {
    var policy = new CacheItemPolicy();
    policy.SlidingExpiration = TimeSpan.FromSeconds(10);

    // this works
    //policy.RemovedCallback = Removed;

    // this creates the exception
    policy.UpdateCallback = Update;

    MemoryCache.Default.Add("test","123",policy);
    Console.Read();
  }

  static void Update(CacheEntryUpdateArguments arguments) { }
  static void Removed(CacheEntryRemovedArugments arguments) { }
}

解决方法

根据文档,您应该使用Set而不是Add.

MemoryCache.Add:

The Add and AddOrGetExisting method overloads do not support the UpdateCallback property. Therefore,to set the UpdateCallback property for a cache entry,use the Set method overloads instead.

确实工作没有问题:

MemoryCache.Default.Set("test",policy);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读