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 am finished with the files, I delete th开发者_运维技巧e 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?
@fyjham is right, you need to move your upload folder outside the webroot
. If this is a public-facing site, having it under the webroo
t is a security risk to begin with.
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.
精彩评论