Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 完全无法定义System.Runtime.Caching的UpdateCallback_C#_.net_Caching - Fatal编程技术网

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

C# 完全无法定义System.Runtime.Caching的UpdateCallback,c#,.net,caching,C#,.net,Caching,我在使用System.Runtime.Caching库的CacheEntryUpdateCallback委托时遇到困难。无论何时定义和设置回调,我都会得到一个ArgumentException,即“CacheItemUpdateCallback必须为null”。为什么它必须为空?我应该能够设置这个,然后得到回调 当使用CacheEntryRemovedCallback委托时,我不明白这一点。我可以在我所有的项目中可靠地重现这一点。我做错什么了吗?下面是一个小示例应用程序: using Syste

我在使用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

:

Add
AddOrGetExisting
方法重载不支持UpdateCallback属性。因此,要为缓存项设置UpdateCallback属性,请改用
set
方法重载

以下各项工作确实没有问题:

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

太好了,这正是我想要的。