IIS 7.0删除子目录时如何防止AppDomain回收?

发布于 2024-08-18 02:45:28 字数 1511 浏览 11 评论 0原文

我有一个 ASP.Net Web 应用程序,允许用户将文件上传到与 Web 应用程序位于同一虚拟目录中的 uploads 目录。每个上传的文件都会进入一个以用户会话 ID 命名的临时子目录。完成文件后,我删除临时子目录。唯一的问题是,当删除子目录时,AppDomain 会被回收并终止所有用户会话(使用inproc 会话状态)。罪魁祸首似乎是一个FileChangesMonitor,它监视应用程序中所有子目录的更改。

以下代码在 Windows Server 2003 上运行的 IIS 6.0 中运行良好,可禁用子目录的 FileChangesMonitor,但由于某种原因,它在 Windows Server 2008 上的 IIS 7.0 中不起作用:

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[] { });

我找到了另一个禁用FileChangesMonitor 总共 此处。但这不是理想的解决方案,因为我仍然想监视除 uploads 目录中的临时子目录之外的所有其他文件。

为什么这在 IIS 6.0 中有效,而在 IIS 7.0 中无效?

在 IIS 7.0 中,您可以在要禁用回收的虚拟文件夹中指定子目录吗?

有没有另一种方法可以在不使用反射的情况下做到这一点?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-08-25 02:45:28

@fyjham 是对的,您需要将上传文件夹移到 webroot 之外。如果这是一个面向公众的网站,那么将其置于 webroott 下首先就会存在安全风险。

@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.

ㄟ。诗瑗 2024-08-25 02:45:28

为了回答我自己的问题,我尝试了 IIS 7.0,发现当应用程序池上的托管管道模式设置为“集成”时,上述反射代码不起作用。请确保将应用程序池的托管管道模式设置为“经典”。

似乎也没有办法通过 IIS 手动禁用此功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文