如果在 page_load 事件中调用 base.OnLoad() 会导致无限循环吗?
我团队的一名成员浏览了 ASP.NET Web 应用程序中的几个页面,并将一些 OnLoad 重写更改为 page_load 事件,但他没有删除对 base.OnLoad() 的调用。
这:
Public void override OnLoad()
{
//stuff
base.OnLoad();
}
对此:
Public void Page_Load(object sender, EventArgs e)
{
//stuff
base.OnLoad();
}
注意:如果有语法错误,我深表歉意,我不在具有实际源代码的计算机上。
当我们将代码推送到实时服务器时,我们开始遇到 IIS app_pool 每 45 分钟到一小时崩溃的问题。我们仍然不完全确定这是问题所在,但我很好奇 page_load 事件是从哪里调用的。它们是从 system.web.ui.page 中的 OnLoad 方法调用的吗?如果是这样,那么我认为这会导致无限循环并最终耗尽内存并使应用程序池崩溃。
这会是我们遇到麻烦的原因吗?
A member of my team went through a few pages in our ASP.NET web application and changed some OnLoad overrides to page_load events, but he did not remove the call to base.OnLoad().
This:
Public void override OnLoad()
{
//stuff
base.OnLoad();
}
To this:
Public void Page_Load(object sender, EventArgs e)
{
//stuff
base.OnLoad();
}
Note: I apologize if there are syntax errors, I am not on a computer with the actual source code.
When we pushed out code to the live server we started having issues with the IIS app_pool crashing every 45 mins to an hour. We are still not entirely sure this was the issue but I am curious where page_load events get invoked from. Do they get invoked from the OnLoad method in the system.web.ui.page? If so then I have the opinion this was causing an infinite loop and eventually running out of memory and crashing the app_pool.
Could this be the cause of our troubles?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
base.OnLoad();
导致引发Load
事件。然后,这将导致引发页面的OnLoad
事件处理程序,其中包含再次对OnLoad
的调用。您发布的代码确实不正确。MSDN 上的 ASP.NET 页面生命周期概述文章值得一读用于解释页面加载和其他事件如何工作。
base.OnLoad();
causes theLoad
event to be raised. That would then cause your page'sOnLoad
event handler to be raised, which contains the call toOnLoad
again. The code you've posted is indeed incorrect.The ASP.NET Page Life Cycle Overview article on MSDN is a good read for explaining how page loads and other events work.
即使它不会导致无限循环,从重写基本虚拟方法到处理事件的决定也不是一个明智的决定。我建议您重写,而不是担心无限循环。事实上,它让你担心,这表明覆盖是一个更好的选择。
阅读 Bill Wagner 的 Effective C# 中的第 30 项:优先选择事件处理程序。
Even if it doesn't cause infinite loop, the decision of change from overriding the base virtual method to handling events, is not an intelligent one. I would suggest you to override, instead of worrying about infinite loops. The fact that it makes you worry, is an indication that overriding is a better choice.
Read Item 30 : Prefer Overrides to Event Handlers from Effective C# by Bill Wagner.