在asp.net中关闭Global.asax中的文件/文件夹更改

防病毒扫描.Net部署的文件夹。 因此,应用程序会经常为客户注销。

需要大量批准才能在项目的文件夹级别获得豁免。 所以,我使用下面的代码:

//FIX disable AppDomain restart when deleting subdirectory //This code will turn off monitoring from the root website directory. //Monitoring of Bin, App_Themes and other folders will still be operational, so updated DLLs will still auto deploy. System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); object o = p.GetValue(null, null); System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase); object monitor = f.GetValue(o); System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); m.Invoke(monitor, new object[] { }); 

将这篇文章用于上面的代码。

代码工作正常一天。 但是,问题第二天又开始了。 所以,我再次替换部署的文件夹。 然后,一切正常。

想知道是什么原因导致问题重新开始。 应该怎样做才能再次面对。

此外,如何停止扫描部署应用程序的所有文件夹。 因为,应用程序具有自定义文件夹,其中将保存输出文件。 这也不应该被扫描。

提前致谢!

这是因为你的应用程序池被IIS回收(这是一件好事,因为它可以防止你可能有任何内存泄漏)。

发生这种情况时,不再调用Application_start : https : //msdn.microsoft.com/en-us/library/ms178473.aspx (同样适用于IIS 7.0+)

您应该做的是在Global.asax设置一个static字段,并在每个Application_AcquireRequestState检查它是否设置为true 。 如果没有,请运行您发布的代码,然后将该字段设置为true。

这将确保代码只有在您的工作进程处于活动状态时才会运行一次。

 private static bool hasRemovedFoldersFromMonitoring = false; protected void Application_AcquireRequestState(object sender, EventArgs e) { if(!hasRemovedFoldersFromMonitoring){ System.Reflection.PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); object o = p.GetValue(null, null); System.Reflection.FieldInfo f = o.GetType().GetField("_dirMonSubdirs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.IgnoreCase); object monitor = f.GetValue(o); System.Reflection.MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); m.Invoke(monitor, new object[] { }); hasRemovedFoldersFromMonitoring = true; } } 

由于FileChangesMonitor类中只有2个监视器,因此您只需要停止对两者的监视,因此完整列表将是:

 // Add at the top of the file using System.Reflection; // anywhere private static bool hasRemovedFoldersFromMonitoring = false; private void StopMonitoring(FileChangesMonitor fcm, string monitorName) { var f = typeof(FileChangesMonitor).GetField(monitorName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase); var monitor = f.GetValue(fcm); var m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance |BindingFlags.NonPublic); m.Invoke(monitor, new object[] { }); } protected void Application_AcquireRequestState(object sender, EventArgs e) { if(!hasRemovedFoldersFromMonitoring){ var fcmPi = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); var fcm = (FileChangesMonitor)fcmPi .GetValue(null, null); this.StopMonitoring(fcm, "_dirMonSubdirs"); this.StopMonitoring(fcm, "_dirMonAppPathInternal"); hasRemovedFoldersFromMonitoring = true; } } 

非常感谢@zaitsman的帮助

在他的帮助下,修改了如下代码

 <%@ import namespace="System.Reflection"%> private static bool hasRemovedFoldersFromMonitoring = false; protected void Application_AcquireRequestState(object sender, EventArgs e) { if (!hasRemovedFoldersFromMonitoring) { PropertyInfo fcmPi = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); object o = fcmPi.GetValue(null, null); FieldInfo fi_dirMonSubdirs = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase); object monitor_dirMonSubdirs = fi_dirMonSubdirs.GetValue(o); MethodInfo m_dirMonSubdirs = monitor_dirMonSubdirs.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic); m_dirMonSubdirs.Invoke(monitor_dirMonSubdirs, new object[] { }); FieldInfo fi_dirMonAppPathInternal = o.GetType().GetField("_dirMonAppPathInternal", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase); object monitor_dirMonAppPathInternal = fi_dirMonAppPathInternal.GetValue(o); if (monitor_dirMonAppPathInternal != null) { MethodInfo m_dirMonAppPathInternal = monitor_dirMonAppPathInternal.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic); m_dirMonAppPathInternal.Invoke(monitor_dirMonAppPathInternal, new object[] { }); } hasRemovedFoldersFromMonitoring = true; } } 

希望代码可以帮助某人

但是,我最初在访问monitor_dirMonAppPathInternal对象时收到null错误。 如果有人可以说对象何时不为null,那将会有所帮助