0

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[] { });
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
shiva
  • 1
  • 3
  • 2
    There are many other reasons for the application pool to recycle, some of which can't be disabled. The correct solution would really be to make your application work well with recycles instead of attempting to disable them. – Joachim Isaksson Jun 16 '13 at 05:23
  • The right solution is not to delete directories under the application root. – Andrey Shchekin Jun 16 '13 at 05:24
  • but this code often work correctly. – shiva Jun 16 '13 at 05:47
  • @shiva You may can prevent it **sometimes**, but you **can't** prevent it **always**. You have to live with this. – user1908061 Jun 16 '13 at 05:55
  • finally I found the solution . :) Read This Article . [Prevent app restarts and site downtime when deploying files ](http://beweb.pbworks.com/w/page/30073098/Prevent%20app%20restarts%20and%20site%20downtime%20when%20deploying%20files) – shiva Jun 16 '13 at 10:16
  • Good for you. It's still not possible, under no circumstances. It WILL eventually recycle. How about fixing the cause, instead of trying to work around the symptoms? – user1908061 Jun 16 '13 at 10:18
  • tnx for your help . my problem solved and After Deleting a directory cache will not clear . – shiva Jun 16 '13 at 10:32

1 Answers1

1

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.

user1908061
  • 616
  • 5
  • 12