views:

245

answers:

2

I have an asp.net web application that allows users to upload files to an 'uploads' directory that is located in the same virtual directory as the web app. Each uploaded file goes into a temporary sub directory that is named after the user's session id. Once I'm finished with the files, I delete the temp sub directory. The only problem is that when a sub directory is deleted, the AppDomain gets recycled and kills all user sessions (using inproc session state). The culprit appears to be a FileChangesMonitor that watches for changes in all sub directories in the application.

The following code works great in IIS 6.0 running on Windows Server 2003 to disable the FileChangesMonitor for sub directories, but for some reason it's not working in IIS 7.0 on Windows Server 2008:

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

I found another solution that disables the FileChangesMonitor altogether here. But this is not the ideal solution as I still want to monitor all other files except for the temp sub directories in the 'uploads' directory.

Why does this work in IIS 6.0 and not in IIS 7.0?

In IIS 7.0 can you specify sub directories in a virtual folder you want to disable recycling on?

Is there another way to do this without using reflection?

+2  A: 

fyjham is right... you need to move your upload folder outside the webroot. If this is a public-facing site, having it under the webroot is a security risk to begin with.

Bryan
A: 

To answer my own question, I played around with IIS 7.0 and found out that when the managed pipeline mode was set to 'Integrated' on the application pool, the above reflection code did not work. Make sure to set your application pool's managed pipeline mode to 'Classic' instead.

There also appears no way to disable this manually through IIS either.

Crackerjack
I'm having a similar problem. However, I found the StopMonitoring changes worked for me whether or not I use Integrated or Classic pipeline. http://stackoverflow.com/questions/2248825/asp-net-restarts-when-a-folder-is-created-renamed-or-deleted
frankadelic