我的缓存项在插入后立即被删除的原因是AppDomain 被卸载第一次调用该网站后。通过捕获卸载事件AppDomain
,我已经能够知道关闭原因了。这防病毒软件正在扫描触发该网站的文件FileChangesMonitor
事件的AppDomain
然后触发了卸载AppDomain.
以下是如何检测 AppDomain 被卸载的原因:
全局.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.DomainUnload += DomainUnloadEventHandler;
}
static void DomainUnloadEventHandler(object sender, EventArgs e)
{
var httpRuntimeType = typeof(HttpRuntime);
var httpRuntime = httpRuntimeType.InvokeMember(
"_theRuntime",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField,
null, null, null) as HttpRuntime;
var shutDownMessage = httpRuntimeType.InvokeMember(
"_shutDownMessage",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
null, httpRuntime, null) as string;
string shutDownStack = httpRuntime.GetType().InvokeMember(
"_shutDownStack",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
null, httpRuntime, null) as string;
}
}
这是什么shutDownMessage
变量包含:
_shutDownMessage: Change Notification for critical directories.
bin dir change or directory rename
HostingEnvironment initiated shutdown
HostingEnvironment caused shutdown
Change in C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\48a6542f\e317ebf6\hash\hash.web
你有没有看到,hash.web文件是 AppDomain 卸载的原因。现在,谁在修改这个文件?原来是防病毒软件。经过停用 McAfee 的按访问扫描程序,hash.web 文件不再更改,因此没有 AppDomain 卸载。问题解决了!
欲了解更多信息,您可以阅读此内容博客文章 http://blogs.msdn.com/b/tess/archive/2008/05/14/asp-net-performance-issue-large-number-of-application-restarts-due-to-virus-scanning.aspx.