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

509 Views Asked by At

After deleting a directory in asp.net the application will restart and I will lose all my session and the cache will clear. 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. 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[] { });
1

There are 1 best solutions below

0
user1908061 On

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.

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.