【问题标题】:Turn off Files/Folder changes in Global.asax in asp.net在asp.net中关闭Global.asax中的文件/文件夹更改
【发布时间】:2018-04-20 17:34:48
【问题描述】:

防病毒软件会扫描 .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[] { }); 

上面的代码使用了这篇文章。

代码可以正常运行一天。但是,第二天问题又开始了。因此,我再次替换了已部署的文件夹。然后,一切正常。

想知道问题再次出现的原因。应该怎么做才能不再面对。

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

提前致谢!

【问题讨论】:

  • 你把代码放在哪里了?
  • global.asaxApplication_Start事件中

标签: c# asp.net iis iis-7 global-asax


【解决方案1】:

这是因为您的应用程序池被 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;
    }
}

【讨论】:

  • 感谢您的回答。你的意思是这样void AcquireRequestState(object sender, EventArgs e) { isAcquireRequestState = true;}。在Application_start 事件中,调用上面发布的代码insdie 的if 条件仅if (isAcquireRequestState == true)isAcquireRequestState 被全局声明
  • 非常感谢您提供的代码。它会阻止扫描其他自定义文件夹吗?希望项目中的所有文件夹和文件免于监视文件更改。再次感谢您的帮助!
  • @KarthickRaju tha 与您发布的代码相同,我只是将其包装不同。所以之前被排除的东西,现在应该被排除。
  • 是的,@zaitsman,在问题中,我还询问了如何免除项目部署的其他文件夹。我只是使用了 OP 中提到的 url 中的代码。我不确定如何排除所有文件夹。你能帮帮我吗?
  • 我在void StopMonitoring(FileChangesMonitor fcm, string monitorName) 收到错误消息。出现错误 - System.Web.FileChangesMonitor 由于其保护级别而无法访问。已经导入了 System.Web
【解决方案2】:

非常感谢@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 错误。如果有人能说对象何时不为空,那将很有帮助

【讨论】:

    猜你喜欢
    • 2014-03-23
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2011-05-11
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多