MemoryCache Empty : Returns null after being set MemoryCache Empty : Returns null after being set asp.net asp.net

MemoryCache Empty : Returns null after being set


So, here's some news. We looked into this and YES, this is a bug in .NET 4.

The good news is that it was fixed in .NET 4.5, so if you can, update your installation to .NET 4.5 and you're solid.

The other good news it that this fix has been back-ported to .NET 4 and will be available as a QFE (Quick Fix...a one off fix you'll apply) #578315. It was backported/fixed just days ago and it should be out ASAP. I'll try to get an exact date, but it's soon.

The other other good news is that there's a workaround for this on .NET 4 before the QFE. The workaround is weird, but it could unblock you.

using (ExecutionContext.SuppressFlow())     {          // Create memory cache instance under disabled execution context flow         return new YourCacheThing.GeneralMemoryCache(…);}

Hope this helps.

UPDATE: The Hotfix is http://support.microsoft.com/kb/2828843 and you can request it here: https://support.microsoft.com/contactus/emailcontact.aspx?scid=sw;%5BLN%5D;1422


We have the same problem. I confirm that after some period of time cache became disposed. It's private field _disposed became 1. I am sure that I don't have call to cache.Dispose in my code. But when I looked at code of MemoryCache with Reflector I saw, that in constructor it subscribes on two events

domain.DomainUnload += eventHandler;domain.UnhandledException += exceptionEventHandler;private void OnAppDomainUnload(object unusedObject, EventArgs unusedEventArgs){  this.Dispose();}private void OnUnhandledException(object sender, UnhandledExceptionEventArgs eventArgs){  if (!eventArgs.IsTerminating)    return;  this.Dispose();}

Both of these event handlers have call to Dispose. May be after some domain recycling in IIS it causes domain unload, but keeps cache in memory(i'am not shure if it is possible).


I have been experiencing the exact same symptoms. I have finally resulted to using the System.Web.Cache class instead and hooking into HttpContext.Cache. It has been working perfectly for the last 3 days..