HTTPModules and Global.asax - ASP.NET Page Life Cycle

I read a great article on the Asp.Net life cycle http://www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle .

I understand that the request goes through MODULE (BeginRequest, authentica, author, prehandler ...) HANDLER (Proccessrequest) PAGE (Init, viewstate, load, render, ......, unload) MODULE (Posthandler, postrequescache, Endreques )

Where exactly is Global.asax (Application_start, Application_begin, ....) located in the stream above? Clarity really helps.

Does the Init function call the first or Global.asax functions?

Thank you for your precious time.

+4
source share
3 answers

ASP.NET applications in IIS are structured as shown in the figure below. I know this probably looks scary, but the names should sound familiar. I hope familiar names will make it a little more digestible.

I am not going to rephrase in words the structure that you see below. The picture does a better job than I could ever say in the sentences. Instead, I come to the consequences that the image has for your questions.

Scary stuff

Application domain
What is an application domain? This is a private allocation of system memory for the application. All code within the domain uses the allocated memory of the domain. This means that static types and references are shared in the domain. No code outside the domain can access this domain memory.

Each ASP.NET application runs inside the application domain for each application pool to which it belongs. This one-to-one relationship is true regardless of the number of threads in the application pool.

Global.asax
What is Global.asax? In the simplest case, it is a .NET class that inherits from System.Web.HttpApplication . HttpApplication provides Global.asax smartmats to manage all HTTP requests through the request pipeline. It will kill all the events of the request life cycle and call ProcessRequest on the handler.

Each ASP.NET application will create multiple instances of HttpApplication (Global.asax). When the request is received, it will be passed to one of the HttpApplication instances. Then the request will remain with the same HttpApplication instance for life. This means that one instance of HttpApplication is processed per request. Each HttpApplication instance can and will be reused to process multiple requests throughout its life.

Events>
Where are application events like Application_Start related? This depends on some of these events being part of the application domain, and some being part of HttpApplication . Application_Start and Application_End refer to the beginning and end of the application domain. Other application events (for example, Application_Begin) are related to the life cycle of the HttpApplication instance.

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

+11
source

Asp.net application lifecycle events draw attention to global.asax. The page life cycle has its own events. more details here:

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

+1
source

HTTP modules against Global.asax files

You can implement most of the module functionality in the Global.asax application file, which allows 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 in applications. See the Global Assembly Cache for more information. However, the advantage of using the Global.asax file is that you can place code in other registered module events, such as Session_Start and Session_End. In addition, the Global.asax file allows you to create global objects available throughout the application. You must use the module when you need to create code that depends on the events of the application, and you either want to reuse the module in other applications, or do not want to place complex code in the Global.asax file. You must place the 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 in applications or when you need to subscribe to events such as Session_Start that are not available to modules.

Introduction to HTTP Modules

0
source

Source: https://habr.com/ru/post/1445190/


All Articles