When i cache an object to the HttpContext Cache
, my object disappear on the next read and I must reinsert the object a second time (not always but 90% of the time). After that, the object stay there without any problem.
Here's what happen:
- Start debugging the mvc project
- Read the age from the cache
- The age is null so i put 50 in the variable and then insert it in the cache
- The
CacheItemRemovedCallback
gets executed immediately when the response to the client is done. TheCacheItemRemovedReason
value isRemoved
- The user click refresh immediately
- Read the age from the cache
- The age is still null so i put 50 in the variable and then insert it in the cache
- The user click refresh immediately
- Read the age from the cache
- The age is there finally!
So why the Cache
have this problem to keep the object in cache on the first insert?
This behavior exist in the .Net framework 3.5, 4.0, 4.5, 4.5.2
.
Here's the code:
public class HomeController : Controller
{
public ActionResult Index()
{
int? age = this.HttpContext.Cache.Get("age") as int?;
if (age == null)
{
age = 50;
this.HttpContext.Cache.Add("age", age, null, DateTime.Now.AddHours(5), TimeSpan.Zero, CacheItemPriority.Default, new CacheItemRemovedCallback(this.CacheItemRemovedCallback));
}
return View();
}
public void CacheItemRemovedCallback(String key, Object value, CacheItemRemovedReason reason)
{
}
}
The reason why my cached item was removed immediately after his insertion was because the AppDomain get unloaded after the first call to the website. By catching the unload event of the
AppDomain
, i've been able to know the shutdown reason. The antivirus was scanning a file of the website that triggered theFileChangesMonitor
event of theAppDomain
that then triggered the unload of the AppDomain.Here's how to detect the reason why the AppDomain get unloaded :
Global.asax
Here's what the
shutDownMessage
variable contains:Has you can see, the hash.web file is the cause of the AppDomain unload. Now, who is altering this file? It turns out to be the antivirus. By deactivating the On-Access scanner of McAfee, the hash.web file was not altered anymore so no AppDomain unload. Problem solved!
For further information, you can read this blog post.