HTTPModules and Global.asax -- ASP.NET Page Life cycle
Asked Answered
H

3

8

I have read the beautiful article about Asp.Net Page life Cycle http://www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle.

My understanding is the request passes through MODULE(BeginRequest,authentica,author,prehandler...) HANDLER(Proccessrequest) PAGE(Init,viewstate,load,render,......,unload) MODULE(Posthandler,postrequescache,Endreques).

Where exactly does the Global.asax(Application_start,Application_begin,....) comes in the above flow ? Clarity on this will really help

Does Init function called first or Global.asax functions ?

Thanks for your valuable time.

Hawkweed answered 10/11, 2012 at 6:45 Comment(2)
Would you still be interested in an answer to your question? I know it has been a few months.Bundestag
Cool, I'll write one up later today once I gather my thoughts.Bundestag
B
13

ASP.NET applications in IIS are structured like my image below. I know it is probably scary looking but the names should sound familiar. Hopefully the familiar names make it a little more digestible.

I'm not going to rehash in words the structure you see below. The picture does a better job than I could ever say in sentences. Instead, I'll jump right in to the implications the image has for your questions.

Scary Stuff

App Domain
What is an App Domain? It is as a private allotment of system memory for an application. All code inside the domain uses the allocated domain memory. This means static types and references are shared in a domain. No code outside of the domain can access this domain's memory.

Every ASP.NET application runs inside an App Domain for each App Pool it belongs to. This one-to-one relationship holds true regardless of the thread count in an App Pool.

Global.asax
What is Global.asax? At its simplest it is a .NET class that inherits from System.Web.HttpApplication. HttpApplication gives Global.asax the smarts to guide all HTTP Requests through the request pipeline. It will fire all the request life-cycle events and call ProcessRequest on the handler.

Each ASP.NET application will create multiple instances of HttpApplication (Global.asax). When a request is received it will be handed to one of the HttpApplication instances. The request will then stay with the same HttpApplication instance for its lifetime. This means there is one HttpApplication instance per request being handled. Every HttpApplication instance can, and will, be reused to handle many requests during its lifetime.

Aplication Events
Where do the Application events like Application_Start tie in? That depends since some of these events refer to the App Domain and some to HttpApplication. Application_Start and Application_End refer to the start and end of the App Domain. The rest of the Application events (e.g. Application_Begin) refer to the life-cycle of an HttpApplication instance.

More Information
For more information I suggest this MSDN article and this non-MSDN article.

Bundestag answered 3/7, 2013 at 14:41 Comment(0)
H
1

asp.net application life cycle events pay attention to global.asax. The page life cycle has it's own events. read more here:

http://msdn.microsoft.com/en-us/library/ms178473.aspx

Hegyera answered 11/11, 2012 at 13:35 Comment(0)
T
1

HTTP Modules versus Global.asax Files

You can implement much of the functionality of a module in the application's Global.asax file, which enables you to respond to application events. However, modules have an advantage over the Global.asax file in that they are encapsulated and can be created once and used in many different applications. By adding them to the Global Assembly Cache (GAC) and registering them in the Machine.config file, you can reuse them across applications. For more information, see Global Assembly Cache. However, the advantage to using the Global.asax file is that you can place code in other registered module events such as Session_Start and Session_End methods. In addition, the Global.asax file enables you to instantiate global objects that are available throughout the application. You should use a module whenever you need to create code that depends on application events and you either wish to reuse the module in other applications or you don't want to place complex code in the Global.asax file. You should place code in the Global.asax file whenever you need to create code that depends on application events and you do not need to reuse it across applications, or when you need to subscribe to events such as Session_Start that are not available to modules.

Introduction to HTTP Modules

Thoer answered 6/2, 2017 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.