本文介绍了高级:多少次的HttpModule init()方法中得到应用程序的生命叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Web应用程序初始化如下:

Web application initialization is as follows:


  1. 我们知道当IIS接收到特定的Asp.net应用程序资源的第一个请求,IIS创建的 A 的HttpApplication (定义一个实例Global.asax中 codebehind)。

  2. 当创建新实例,它的初始化碰巧也检查所有配置的HTTP模块。

  3. 所有的模块,然后实例化,并把应用程序的模块集合(类型为 HttpModuleCollection

  4. 模块通过循环和的init()方法被调用(当他们注册请求事件)

  1. As we know when IIS receives the first request for a particular Asp.net application resource, IIS creates an instance of a HttpApplication (defined in global.asax codebehind).
  2. When this new instance is created it's initialization happens that also checks all configured HTTP modules.
  3. All modules are then instantiated and put in the application's Modules collection (of type HttpModuleCollection)
  4. modules are looped through and their Init() method is called (when they register for request events)

据我了解了上面的场景发生在Web应用程序启动/初始化(因此应用程序启动的事件)。

As far as I understand it the above scenario happens when a web application is started/initialized (hence application start event).

难道他们(重新)instatiated对每个请求或从模块属性重用每个连续的请求,而Web应用程序还活着吗?据我了解IIS和Asp.net他们是通过一个Web应用程序的整个生命周期重用。

Are they (re)instatiated on each request or reused from the Modules property on each consecutive request while the web application is alive? As I understand IIS and Asp.net they are reused through the whole life of a web application.

如果重复使用它们,我们可以假定它们的的init()方法实际上是应用虚拟事件处理程序开始的事件吗?问题是,我们不能HTTP模块内连接到应用程序级别的事件。但是,如果他们被重用,我们可以使用的init()为应用程序启动事件做任何我们会把在的Global.asax 代替。

If they are reused, can we assume that their Init() method is actually a pseudo event handler for application start event? The thing is we can't attach to application level events within http modules. But if they are being reused we could use Init() as application start event and do whatever we'd put in global.asax instead.

我们能否假设模块的的init()方法被调用的在应用程序启动的事件?我们可以用这个假设来即寄存器路线,其应用的Global.asax codebehind我们不能改变? 的web.config 通常是访问,我们可以改变它,我们想要的方式。结果
这是否实际工作?

Can we assume that module's Init() method is called only on application start event? Could we use this assumption to i.e. register routes for applications whose global.asax codebehind we can't change? web.config is usually accessible and we can change it the way we want.
Would this actually work?

我们可以检查的HttpApplication code,检查其 InitModulesCommon()方法。这一次实际上是调用每个注册的HTTP模块的init()。什么是更有趣的是,这种方法仅用于 InitIntegratedModules() InitModules()方法。这两者都在 HttpApplication.InitInternal()方法使用的。这是我的假设的基础上,但我想知道是否有人滥用 IHttpModule.Init()应用程序启动的事件。

We can check HttpApplication code and check its InitModulesCommon() method. This one actually calls Init() of each registered HTTP module. What is more interesting is that this method is only used by InitIntegratedModules() and InitModules() methods. Which are both used only in HttpApplication.InitInternal() method. This is the basis of my assumptions, but I would like to know whether someone has abused IHttpModule.Init() for application start event.

推荐答案

我测试 IHttpModule的初始化的内部工作后如下:

Init() is called only once (per HttpApplication instance)

After I tested this the inner workings of IHttpModule initialization are as follows:


  1. 每个 IHttpModule的通过instatiating在Web应用程序启动初始化和调用的init()

  2. 的HttpApplication 存储在其模块属性中的所有模块实例

  3. 模块是那么的整个生命重用的HttpApplication 并没有丢弃/重新初始化,只要应用程序是活

  1. Every IHttpModule is initialized at web application start by instatiating and a call to Init() method
  2. HttpApplication stores all module instances in its Modules property
  3. Modules are then reused for the whole life of an HttpApplication and are not discarded/reinitialized as long as the application is alive

您不能将一个 IHttpModule的应用程序级别的事件,但您可以使用它的的init()方法模拟应用启动事件委托。里面你可以执行任何code,你会通常把里面的的Application_Start 委托在的Global.asax

So the best outcome is

You can't attach an IHttpModule to application level events, but you can use its Init() method as pseudo application start event delegate. Inside it you can execute any code that you'd usually put inside Application_Start delegate in your Global.asax.

您还可以阅读关于它的详细信息,在我的它会告诉你如何以及何时使用螺纹锁固与你的模块,使之能作为一个 Application_OnStart的事件处理程序。 BTW:这也是可能的,如果你需要办理 Application_OnEnd 事件。 ;)

So whenever your module modifies some common shared resource, you should take extra measures to indicate that the first module has done that and others won't do it again. Read an additional blog post about it that will show you how and when to use thread locking with your module to make it actually act as an Application_OnStart event handler. BTW: It's also possible to handle Application_OnEnd event if you need to. ;)




  1. Writing a custom IHttpModule that handles Application_OnStart event
  2. How to correctly use IHttpModule to handle Application_OnStart event

这篇关于高级:多少次的HttpModule init()方法中得到应用程序的生命叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:09