如何防止 ASP.NET 应用程序在 web.config 修改后重新启动?
我通过 ApplicationHost.CreateApplicationHost
方法托管 ASP.NET 运行时。 当我在应用程序运行时修改 web.config
时,我看到很多第一次抛出 ThreadAbortException
的机会。 这是在我的应用程序崩溃之前。 我假设这是因为运行时已检测到配置更改并想要重新启动。
这对我们来说并不是真正受支持的场景,所以我希望我可以关闭自动重新加载。
有谁知道如何做到这一点?
I'm hosting the ASP.NET runtime via the ApplicationHost.CreateApplicationHost
method. When I modify the web.config
while the application is running, i see lots of first chance ThreadAbortException
s thrown. This is right before my application comes crashing down. I'm assuming this is because the runtime has detected changes to the configuration and wants to restart.
This isn't really a supported scenario for us, so i'd prefer if I could just switch off the automatic reloading.
Does anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实上,前两个答案都是错误的。 防止这种回收发生是可能的,而且非常容易,并且该功能至少从 IIS6 开始就可用。
方法 1(系统范围)
将
HKLM\SOFTWARE\Wow6432Node\Microsoft\ASP.NET\FCNMode
的DWORD
注册表设置更改为值1
,这将禁用所有文件更改通知。不要对位置感到困惑:在这种情况下,
Wow6432Node
对 Web 应用程序的位数没有影响。方法 2 (.NET 4.5+)
如果您使用 .NET 4.5,则现在可以要在每个站点级别禁用此功能,只需在
web.config
中使用以下内容:方法 3 (IIS6+)
最后,并且(至少)自 IIS6 以来,有一个名为
DisallowRotationOnConfigChange
< /a> 仅作为应用程序池的设置(至少我认为 MSDN 上的文本是这么说的,但我还没有测试过)。 将其设置为true
,对应用程序池配置的更改不会导致立即回收。最后一个设置也可以从应用程序池的高级设置中进行设置:
方法 4 (ASP.NET 1.0 和 1.1)
对于使用 ASP.NET 1.0 或 1.1 的(旧)网站,有一个已确认的bug 可能会导致文件更改快速重复回收。 当时的解决方法类似于 MartinHN 在主要问题下建议的内容,即,类似于以下内容的
web.config
:这不会禁用回收,但仅在发生 5000 次重新编译后才会禁用。 该数字是否有用取决于您的应用程序的大小。 微软没有明确说明重新编译到底是什么。 但默认值为 15。
顺便说一句:无论 .NET 或 Windows 的版本如何,我们发现当应用程序从共享运行并在负载平衡环境中使用时,站点会不断回收。 解决这个问题的唯一方法是将 FNCMode 设置添加到注册表(但现在有更细粒度的选项)。
Actually, the first two answers are incorrect. It is possible, and quite easy, to prevent this recycling from happening, and this feature has been available since at least IIS6.
Method 1 (system wide)
Change the
DWORD
registry setting forHKLM\SOFTWARE\Wow6432Node\Microsoft\ASP.NET\FCNMode
to the value1
, which will disable all file change notifications.Don't be confused by the location:
Wow6432Node
has, in this case, no influence on the bitness of your web application.Method 2 (.NET 4.5+)
If you are using .NET 4.5, then it is now possible to disable this on a per-site level, simply use the following in your
web.config
:Method 3 (IIS6+)
Finally, and also (at least) around since IIS6, there's a setting called
DisallowRotationOnConfigChange
as a setting for only the application pool (at least that is what I think the text on MSDN tries to say, but I haven't tested it). Set it totrue
and changes to the configuration of the application pool will not result in an immediate recycle.This last setting can also be set from Advanced Settings of the application pool:
Method 4 (ASP.NET 1.0 and 1.1)
For (old) websites using ASP.NET 1.0 or 1.1, there is a confirmed bug that can cause rapid and repeated recycles on file changes. The workaround at the time was similar to what MartinHN suggested under the main question, namely, something like the following in your
web.config
:This does not disable recycling, but it does so only after 5000 recompilations have taken place. Whether this number is useful depends on the size of your application. Microsoft does not clearly say what a recompilation really is. The default, however, is 15.
As an aside: regardless of the version of .NET or Windows, we find that when the application is run from a share and used in a load-balanced environment, that the site recycles continuously. The only way to solve it was by adding that
FNCMode
setting to the registry (but now there are more fine-grained options).据我所知,没有办法禁用此行为,更改 webconfig 会强制重新启动应用程序。更新:实际上是可能的,有很多方法,有详细记录,正如此答案中的解释*
原始答案:
有一个类似的问题此处仅供其他参考。 我找到了可能有帮助的其他信息。
来自这篇 MSDN 文章
* 免责声明:我写了另一个答案,通常不会进行自我引用,但发现它足够相关,可以链接到这里,因为在这篇文章发布 8 年后,它确实非常不同:通过单击解决方案非常简单IIS 前端,自 ASP.NET 1.0 起就存在解决方法。
As far as I am aware there is no way to disable this behavior, changes to the webconfig force the application to be restarted.Update: it is actually possible, there are a number of methods, well documented, as explained in this answer*
Original answer:
There is a similar question here just for other reference. I found additional info that may be helpful.
From This MSDN Article
* Disclaimer: I wrote the other answer and normally wouldn't make a self-reference, but find it relevant enough to link here since 8 years after this post it is really quite different: a solution is very easy by clicking through the IIS front-end, and workarounds exist since ASP.NET 1.0.
我遇到了同样的更大问题 - 对 AppDomain 基目录中的任何文件或子文件夹的更改都会导致托管环境关闭。 这对于我们的应用程序来说是一个相当大的问题,因为我们在同一个 AppDomain 中运行 WPF UI,并且我们无法在不干扰用户的情况下重新启动它。
我真的想避免为应用程序的基于 Web 的部分运行单独的 AppDomain,因此我使用 Reflector 进行了一些挖掘。 我发现罪魁祸首是内部类
FileChangesMonitor
。所以我写了一个可怕的反射黑客来解决这个问题。 我想我应该将其发布在这里,作为其他遇到同样问题的人的潜在解决方案。 您只需调用 HttpInternals.StopFileMonitoring() 即可禁用文件/文件夹更改时的关闭功能。
I ran in to an even bigger problem along the same lines - changes to any file or sub-folder in the AppDomain base directory cause the hosting environment to shutdown. This is a pretty big issue for our application as we're running a WPF UI in the same AppDomain and we can't restart it without being distruptive to the user.
I really wanted to avoid having to run a separate AppDomain for the web based part of the application so I did some digging with Reflector. I found that the culprit was the internal class
FileChangesMonitor
.So I wrote a horrible horrible reflection hack to solve the problem. I thought I'd post it here as a potential solution for anyone else having the same problem. You just need to call
HttpInternals.StopFileMonitoring()
to disable shutdown on file/folder changes.解决方案是将以下元素添加到 web.config 部分:
A solution would be adding following element to web.config section :
正如 jfburdet 所提到的,解决方案是使用 waitChangeNotification 和 maxWaitChangeNotification。
话虽如此,您应该知道,如果 ASP.NET 在混合模式下运行,它们在 IIS 7 上不起作用: http://forums.iis.net/t/1149344.aspx
As mentioned by jfburdet the solution is to use waitChangeNotification and maxWaitChangeNotification.
That being said, you should know they don't work on IIS 7 if ASP.NET is run in mixed mode: http://forums.iis.net/t/1149344.aspx