Disable Session Expiration when using Directory.Delete()

 As you may already know, one of the reasons why the asp.net worker process recycles which kills all the session variables in memory is due to dynamically deleting a folder under the root application folder. You have two options to follow in this case

1- To use out-of-process mode session
2- To use the below code which uses reflection to disable monitoring for the folders inside the application. In this way, once you delete a folder, the Application domain won't restart thus you won't be losing any session variable

PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public |  BindingFlags.Static);
object o = p.GetValue(null, null);
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[] { });

Do not forget to import System.Reflection Namespace

Hope this helps,

Published 12 November 2008 05:17 PM by haissam


# Bob Pinella said on 19 August, 2009 04:53 PM

Thank you for this!  This was such an obscure error and there is not much information about this on the net.  You should also mention that this code should be placed in the Application_Start method of the global.asax file.

# easy-soon said on 07 October, 2009 12:16 AM

your codes are write in c#.

so i did write in vb.net....


Private Sub FixAppDomainRestartWhenTouchingFiles()

Dim p As System.Reflection.PropertyInfo = GetType(HttpRuntime).GetProperty("FileChangesMonitor", Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)

Dim o As Object = p.GetValue(Nothing, Nothing)

Dim f As System.Reflection.FieldInfo = o.GetType.GetField("_dirMonSubdirs", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.IgnoreCase)

Dim monitor As Object = f.GetValue(o)

Dim m As System.Reflection.MethodInfo = monitor.GetType.GetMethod("StopMonitoring", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)

m.Invoke(monitor, New Object() {})

End Sub

# Keith G said on 03 November, 2009 11:45 PM

Magnificent... i have this problem where i create a thread to do a worker process This process creates a folder + content on each call and ends up calling Directory.Delete() if the folder count gets above 5. fair enough, But on any method call after the Directory.Delete call the thread dies with no explination except "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."

I racked my brain over this many a time thinking i had fixed it but it would keep reappearing. I didnt even know dynamically deleting a folder under the root application folder would cause the webservice to restart. It would be ace if this was mentioned in msdn or similar.

Thanks heaps.

btw i didnt put this in global.asmx however just before the delete method call to make sure that it will never happen afagain even if someone else turns the monitor back on.

# Dani de Saro said on 01 December, 2009 08:39 AM

Fantastic solution! Very thanks!

# Sergey said on 08 December, 2009 04:51 AM

Thank you very much!



# Prabir Shrestha said on 29 December, 2009 11:42 PM

Hi thanks for the info.

But is it possbile to strop monitoring only a particular directory.

For example i dont want to monitor App_Data/tmp

but other directories i i would like to monitor.

# Vandnaa Singh said on 30 December, 2009 12:22 AM

Thanks a lot. This code save me.

# blog from nlab said on 09 March, 2010 08:09 AM

Disable Session Expiration when using Directory.Delete()

This site



This Blog



  • MaximumASP