简体   繁体   English

在ASP.NET 4中删除目录后如何禁用应用程序重新启动?

[英]How to disable application restart after deleting a directory In ASP.NET 4?

After deleting a directory in asp.net the application will restart and I will lose all my session and the cache will clear. 删除asp.net中的目录后,应用程序将重新启动,我将丢失所有会话,并且缓存将清除。 So I found the following solution; 因此,我找到了以下解决方案; I Put the following code in the Application_Start of Global.asax to disable disable application pool recycling, but sometimes it doesn't work. 我将以下代码放入Global.asaxApplication_Start中以禁用禁用应用程序池回收,但有时不起作用。 Why? 为什么?

System.Reflection.PropertyInfo p = typeof(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[] { });

You can't prevent the Application Pool Recycling, and doing so is the wrong way to achieve things anyway. 无法阻止应用程序池回收,无论如何,这样做是错误的方法。

Instead you should not delete any files or directories within your application directory. 相反,您不应删除应用程序目录中的任何文件或目录。 For temporary data you should use the temp directory, for persistent data you should store it in a separate location. 对于临时数据,应使用temp目录,对于持久数据,应将其存储在单独的位置。

Also if you care about your sessions and cache to be persisted after a application pool recycle, you should additionally store it in a persistent data storage and reload it. 另外,如果您关心会话和缓存在应用程序池回收后是否可以保留,则还应该将其存储在持久性数据存储中并重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM