当 HttpModule Init 方法在 ASP.NET 集成模式下运行时?
我编写了一个 HttpModule,它是 NHibernate Session Provider。它只是在 HttpModule 的 Init 方法中打开一个 SessionFactory 并在 BeginRequest 中获取一个新的 Session 并在 EndRequest 中关闭它。另一方面,我在 Global.asax 中编写了一个方法,该方法使用来自此 HttpModule 的名为 GetData 的会话。我在 Global.asax 的 Init 方法中运行该方法(GetData)。问题是当我在集成模式下使用 HttpModule 时,我的 HttpModule 在运行 Global.asax 的 Init 方法之前似乎没有初始化。
我搜索了这些方法的运行顺序和事件的生命周期,但没有发现任何有用的信息!
I've written an HttpModule that is an NHibernate Session Provider. It simply opens a SessionFactory in Init method of HttpModule and gets a new Session in BeginRequest and closes it in EndRequest. At the other side, I wrote a method in Global.asax that uses a session from this HttpModule named GetData. I run that method (GetData) in Init method of Global.asax. The problem is when I use my HttpModule in integrated mode it seems that my HttpModule does not Initialize before running Init method of Global.asax.
I've searched for order of running these methods and life cycle of events but nothing useful found!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 IIS 7.x 中,模块和处理程序应在
web.config
文件的system.webServer
xml 元素中注册。查看此 MSDN 文章:
In IIS 7.x, modules and handlers should be registered within
system.webServer
xml element in yourweb.config
file.Check this MSDN article:
ASP.NET 对于何时相互调用 HttpModule Init() 方法不提供任何保证(与静态初始值设定项不同)。一般来说,Init() 方法应该用于连接事件处理程序,并且任何“实际工作”都应该在事件处理程序中完成。
在继续下一个事件之前,从所有 HttpModule(包括 Global.asax)调用特定事件的所有已注册事件处理程序 - 因此您可以通过这种方式控制顺序。
ASP.NET does not provide any guarantees on when HttpModule Init() methods are called with respect to one another (not unlike static initializers). In general, the Init() methods should be used to wire up event handlers, and any "real work" should be done in the event handlers.
All registered event handlers for a specific event are called from all HttpModules, including Global.asax, before moving on to the next event -- so you have control over order that way.