views:

256

answers:

1

Hi,

Can somebody explain why the constructor of a custom class derived from HttpApplication is called several times upon application startup?

My code structure is the following:
- My Global class in global.asax derives from CustomApp class.
- The CustomApp class derives from HttpApplication class

The Global class is created at startup, but when I place a breakpoint in the constructor, it is invoked several times! I thought there should be only one instance of Application class created?

Am I wrong?

UPD: the web server can indeed create several HttpApplication instances to process multiple requests coming in at the same time. This becomes especially apparent when you place a breakpoint in the constructor of your HttpApplication descendant. Several requests will be pending from the client (http content, CSS files, etc) and to serve each of them the web server will create new instances of HttpApp. So, beware of this, when writing the application initialization logic.

+3  A: 

I believe the ASP.NET runtime may create more than one HttpApplication per application domain. So HttpApplication.Init and the Ctor may get called more than once.

If you want to have initialization code that only runs once, you should use the Application_Start event which will only be called once per app.

McKAMEY
Is there an event (or similar) which I can hook in to for Applcation_Start since I need to have my HttpApplication class outside of the website ?
Saint Gerbil
Just implement a method `Applcation_Start` in Global.asax.cs and it will be automatically wired up and called at start time.http://msdn.microsoft.com/en-us/library/ms178473.aspx
McKAMEY