如果你曾经修改了ASP.NET应用程序(dll文件),与修改了bin文件夹或Web.config文件(添加/删除/重命名的文件等),而该网 站在运行,你可能已经注意到,这将导致在AppDomain的重新启动。所有的会话状态会丢失和网站再次成功启动,任何登录的用户将被退出(假设你不使用 持久Cookie身份验证)。 当然,当我们修改了web.config文件,并保存它,迫使一个AppDomain重新启动,这是我们需要的。

 

我们有时动态创建和删除的文件夹,在ASP.NET 2.0中,文件夹删除将导致一个AppDomain重新启动,这将导致严重的问题。 例如,对于一个电子商务网站的产品,你可能想存储在目录中的产品来自它的名字ID的产品的图片,例如。/ productImages/123/ipod-nano.jpg,甚至为身份证图像的记录。 这有助于避免与其他上载的文件和图像文件名冲突。 当然,当你来到删除从数据库产品,你自然要删除其相应的图像和含有它的文件夹,但显然不能因为这AppDomain重新启动的问题。 因为,我们删除留在我们的服务器中的空文件夹(文件删除不会引起应用程序重新启动)。

解决方案

幸运的是,我们有了Reflection and HttpModules的解决方案。 首先创建一个像.cs文件...

using System.Reflection;   

using System.Web;

namespace MyWebsite
{
 
/// <summary>
 
/// Stops the ASP.NET AppDomain being restarted (which clears
 
/// Session state, Cache etc.) whenever a folder is deleted.
 
/// </summary>
 public class StopAppDomainRestartOnFolderDeleteModule : IHttpModule
 {
    
public void Init(HttpApplication context)
    {
        PropertyInfo p 
= typeof(HttpRuntime).GetProperty("FileChangesMonitor",
        BindingFlags.NonPublic 
| BindingFlags.Public | BindingFlags.Static);

        
object o = p.GetValue(nullnull);

        FieldInfo f 
= o.GetType().GetField("_dirMonSubdirs",
        BindingFlags.Instance 
| BindingFlags.NonPublic | BindingFlags.IgnoreCase);

        
object monitor = f.GetValue(o);

        MethodInfo m 
= monitor.GetType().GetMethod("StopMonitoring",
          BindingFlags.Instance 
| BindingFlags.NonPublic);

        m.Invoke(monitor, 
new object[] { });
     }

     
public void Dispose() 
     { }
   }
}

相关文章:

  • 2021-12-26
  • 2022-01-03
  • 2021-11-06
  • 2022-12-23
  • 2022-02-08
  • 2021-11-18
  • 2022-12-23
猜你喜欢
  • 2022-03-02
  • 2021-09-14
  • 2021-09-11
  • 2022-12-23
  • 2021-12-10
  • 2021-12-10
  • 2022-02-26
相关资源
相似解决方案