Difficulty: Easy
Correct Answer: Correct – Application_Start fires once when the application domain is created in response to the first request
Explanation:
Introduction / Context:
ASP.NET applications run inside an application domain managed by the web server. The Global.asax file exposes events that occur when the application starts and ends. Application_Start is particularly important because it provides a single place to perform initialisation that should happen once for the whole application, such as registering routes or seeding caches. Understanding when Application_Start fires is essential for designing application level setup logic.
Given Data / Assumptions:
Concept / Approach:
Application_Start is raised by ASP.NET when the application domain for the web application is created. This usually happens when the first HTTP request for any resource in the application arrives after the application pool has started or after the application has been recycled. The event is not raised on every request; it is a one time initialisation event per application domain lifetime. Developers use it to perform tasks that should run only once, such as configuring dependency injection containers or loading configuration data into memory.
Step-by-Step Solution:
Step 1: When IIS receives the first request for an application that is not yet loaded, it starts the application pool worker process if necessary and creates an application domain for that site or virtual directory.
Step 2: As part of initialising the ASP.NET runtime in this domain, the Global.asax file is compiled and loaded.
Step 3: The Application_Start event is raised once, giving the application a chance to run global start up code.
Step 4: Subsequent requests reuse the same application domain and do not cause Application_Start to fire again until the application is recycled or restarted.
Step 5: When a recycle or shutdown occurs and a new application domain is created later, Application_Start will fire again for that new lifetime.
Verification / Alternative check:
You can confirm this behaviour by placing logging code inside Application_Start and observing that it appears only once per application domain lifetime, even when many users access pages. If the application pool is recycled or you modify web.config, the application restarts and Application_Start is logged again. Official ASP.NET life cycle diagrams show Application_Start as a one time event during application initialisation.
Why Other Options Are Wrong:
Option B is wrong because Application_Start does not fire on every request; that would make it an application request event, not a start up event. Option C is incorrect because Application_Start is independent of the operating system boot process and is controlled by IIS and the ASP.NET runtime. Option D is clearly wrong because Application_Start belongs to ASP.NET, not SQL Server.
Common Pitfalls:
A common mistake is placing code that depends on per request context, such as HttpContext specific data, inside Application_Start, which may not be appropriate. Another pitfall is performing heavy or blocking operations during Application_Start, causing the first request to have a long delay. Some developers also forget that Application_Start may be called again after a recycle, so any cached data may need to be reloaded. Using Application_Start wisely helps create fast, reliable initialisation logic for the whole application.
Final Answer:
The description is correct, because Application_Start fires once when the ASP.NET application domain is created in response to the first request and is used for global initialisation.
Discussion & Comments