I am wondering under what circumstances I should be putting application initialisation code in Application_Start()
vs Init()
in my Global.asax
file?
The distinction between the two doesn't seem very obvious to me, other than Application_start
gets called first, then Init()
.
So far the only real pointer I can find is that IHttpModule
only has an Init()
method, so if what I'm doing may at some point be better suited to implement IHttpModule
I should use the Init()
method of Global.asax
, if nothing else for consistency.
Application_Start. The Application_Start event is fired the first time when an application starts. Session_Start. The Session_Start event is fired the first time when a user's session is started. This typically contains for session initialization logic code.
Application_Start. Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values.
Global. asax is an optional file which is used to handling higher level application events such as Application_Start, Application_End, Session_Start, Session_End etc. It is also popularly known as ASP.NET Application File. This file resides in the root directory of an ASP.
asax contains code which is executed. Web. config contains configuration settings of web application, for example connection string, included libraries, namespaces, users and groups access permissions to virtual directories, etc.
From the MSDN docs:
The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.
Init:
Called once for every instance of the HttpApplication class after all modules have been created.
UPDATE: if you need to make sure a certain code is called only once in the app. lifecycle, Application_Start is a better solution. Examples: configuring log4net?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With