Failed to determine UpdateCallback for System.Runtime.Caching

It's hard for me to spend time with the CacheEntryUpdateCallback delegate of the System.Runtime.Caching library. Whenever I define and set a callback, I get an ArgumentException that "CacheItemUpdateCallback must be null." Why should it be zero? I would have to set this and then get a callback.

I do not get this when using the CacheEntryRemovedCallback delegate. I can reliably reproduce this in all my projects. Am I doing something wrong? Here is a small sample application:

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) { } } 
+6
source share
1 answer

According to the documentation, you should use Set instead of Add .

MemoryCache.Add :

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

After really work without problems:

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

Source: https://habr.com/ru/post/982285/


All Articles