Overview of the ASP. NET application (application) life cycle

Source: Internet
Author: User
Tags naming convention

Original: ASP. NET application (application) Life cycle overview

Reference MSDN:ASP.NET application Life cycle Overview

This topic provides an overview of the application lifecycle, lists important life-cycle events, and describes how to write code that is appropriate for the application life cycle. in ASP. ASP. NET application to initialize and make it handle requests, some processing steps must be performed. In addition, ASP. NET is only part of the Web server structure that processes requests made by the browser. It is important to understand the application lifecycle so that you can write code at the appropriate life cycle stage to achieve the desired results.

Application Life Cycle Overview

The following table describes the various phases of the ASP. NET application life cycle.

Stage Description

The user requests application resources from the WEB server.

The life cycle of the

ASP. NET application is the starting point for the browser to send requests to the WEB server (for ASP. NET applications, typically IIS). ASP. NET is an ISAPI extension under a WEB server. When a WEB server receives a request, it checks the file name extension of the requested file, determines which ISAPI extension should process the request, and then passes the request to the appropriate ISAPI extension. The file name extensions, such as. aspx,. ascx,. ashx, and. asmx, that are mapped to ASP.

note

If the file name extension is not already mapped to ASP. asp, it will not receive the request. This is important to understand for applications that use ASP. For example, because. htm files are typically not mapped to ASP. NET, ASP. NET will not perform authentication or authorization checks on. htm file requests. Therefore, even if the file contains only static content, if you want ASP. NET to check for authentication, you should also create the file with a file extension that is mapped to ASP. NET, such as the file name extension. aspx.

note

If you want to create a custom handler that serves a specific file name extension, you must map the extension to ASP. NET in IIS, and you must register the handler in the application's Web. config file. For more information, see Introduction to HTTP handlers.

ASP. NET receives the first request to an application.

When ASP. NET receives the first request for any resource in the application, the class named Applicationmanager creates an application domain. Application domains provide application isolation for global variables and allow each application to be uninstalled separately. In the application domain, an instance is created for the class named Hostingenvironment that provides access to information about the application, such as the name of the folder where the application is stored.

The following diagram illustrates this relationship:

If required, ASP. NET also compiles the top-level items in the application, including the application code in the App_Code folder. For more information, see "Compilation Life Cycle" later in this topic.

Create an ASP. NET core object for each request.

After you have created the application domain and instantiated the hostingenvironment object, ASP. NET creates and initializes the core objects, such as HttpContext, HttpRequest, and HttpResponse. The HttpContext class contains objects that are specific to the current application request, such as HttpRequest and httpresponse objects. The HttpRequest object contains information about the current request, including Cookie and browser information. The HttpResponse object contains the response sent to the client, including all rendered output and cookies.

Assigning a HttpApplication object to a request

After all of the core application objects are initialized, the application is started by creating an instance of the HttpApplication class. If the application has a Global.asax file, ASP. NET creates an instance of the Global.asax class (derived from the HttpApplication Class) and uses that derived class to represent the application.

Attention

When you request an ASP page or process in your application for the first time, a new instance of HttpApplication is created. However, to maximize performance, you can reuse HttpApplication instances for multiple requests.

When you create an instance of HttpApplication , all the configured modules are created at the same time. For example, if you configure your application this way, ASP. NET creates a SessionStateModule module. After all the configured modules have been created, the Init method of the HttpApplication class is called.

The following diagram illustrates this relationship:

The request is processed by the HttpApplication pipeline.

The following events are executed by the HttpApplication class when the request is processed. Developers who want to extend the HttpApplication class need to be aware of these events in particular.

  1. Validation of the request checks the information sent by the browser and determines whether it contains potentially malicious tokens. For more information, see ValidateRequest and Scripting Intrusion Overview.

  2. If any URLs have been configured in the Urlmappingssection section of the Web. config file, the URL mapping is performed.

  3. Raises the BeginRequest event.

  4. Raises the AuthenticateRequest event.

  5. Raises the Postauthenticaterequest event.

  6. Raises the AuthorizeRequest event.

  7. Raises the Postauthorizerequest event.

  8. Raises the Resolverequestcache event.

  9. Raises the Postresolverequestcache event.

  10. Depending on the file name extension of the requested resource (mapped in the application's configuration file), select the class that implements IHttpHandler to process the request. If the request is for an object (page) derived from the page class, and the page needs to be compiled, ASP. NET compiles it before creating an instance of the page.

  11. Raises the Postmaprequesthandler event.

  12. Raises the AcquireRequestState event.

  13. Raises the Postacquirerequeststate event.

  14. Raises the PreRequestHandlerExecute event.

  15. Call the appropriate IHttpHandler class's ProcessRequest method (or asynchronous version BeginProcessRequest) for the request. For example, if the request is for a page, the current page instance will process the request.

  16. Raises the PostRequestHandlerExecute event.

  17. Raises the ReleaseRequestState event.

  18. Raises the Postreleaserequeststate event.

  19. If the Filter property is defined, the response filter is executed.

  20. Raises the Updaterequestcache event.

  21. Raises the Postupdaterequestcache event.

  22. Raises the EndRequest event.

Life cycle events and Global.asax files

During the life cycle of an application, an application raises an event that can be handled and invokes a specific method that can be overridden. To handle application events or methods, you can create a file named Global.asax in the application root directory.

If you create a Global.asax file, ASP. NET compiles it to a class that derives from the HttpApplication class and then uses that derived class to represent the application.

An instance of the httpapplication process processes only one request at a time. This simplifies the application's event-handling process because it does not need to be locked when accessing non-static members in an application class. This also allows the request-specific data to be stored in non-static members of the application class. For example, you can define a property in the Global.asax file, and then assign a request-specific value to the property.

By using a naming convention application_event(such as application_beginrequest), ASP. NET can be Global.asax The application event is automatically bound to the handler in the file. This is similar to the method of automatically binding an ASP. NET page method to an event, such as a page's Page_Load event. For more information, see the ASP. NET page life cycle Overview.

The Application_Start and application_end methods are special methods that do not represent HttpApplication events. During the life cycle of an application domain, ASP. NET calls these methods only once, not once for each HttpApplication instance.

The following table lists some of the events and methods that are used during the application life cycle. These events are more than just listed, but these events are most commonly used.

Event or method Description

Application_Start

Called when the first resource, such as a page, is requested in an ASP. The Application_Start method is called only once during the life cycle of the application. You can use this method to perform startup tasks, such as loading data into the cache and initializing static values.

Only static data should be set during application startup. Because instance data can only be used by the first instance of the HttpApplication class that is created, do not set any instance data.

Application_ Event

Thrown at the appropriate time in the application life cycle, see what is listed in the Application Life table earlier in this topic.

Application_Error can be raised at any stage of the application life cycle.

Because the request is shorted, application_endrequest is the only event that can guarantee that each request will be raised. For example, if there are two modules that handle the Application_BeginRequest event, and the first module throws an exception, the application_beginrequest event is not called for the second module. However, the application_endrequest method is always called to make the application clean up resources.

HttpApplication.Init

Once all the modules have been created, they are called once for each instance of the HttpApplication class.

Dispose

Called before the application instance is destroyed. You can use this method to manually release any unmanaged resources. For more information, see Scavenging unmanaged resources.

Application_End

Called once for each application life cycle before uninstalling the application.

Compile life cycle

When a request is made for the application for the first time, ASP. NET compiles the application items in a specific order. The first items to compile are called top-level items. After the first request, the top-level item is recompiled only if the dependency changes. The following table describes the order in which you compile the ASP.

Items Description

App_GlobalResources

Compiles the application's global resources and generates the resource assembly. Any assembly in the application's Bin folder is linked to the resource assembly.

App_webresources

Create and compile a proxy type for the WEB service. The generated WEB reference assembly is linked to the resource assembly, if present.

Configuration file properties defined in the Web. config file

If the configuration file properties are defined in the application's Web. config file, an assembly that contains the profile object is generated.

App_Code

Generate a source code file and create one or more assemblies. All code assemblies and configuration file assemblies are linked to resources and WEB reference assemblies, if any.

Global.asax

Compiles the Application object and links it to all previously generated assemblies.

After compiling the application's top-level entries, ASP. NET will compile the folders, pages, and other items as needed. The following table describes the order in which you compile the ASP.

Items Description

App_LocalResources

If the folder containing the requested item contains the App_LocalResources folder, the contents of the local resource folder are compiled and linked to the global resource assembly.

Individual web pages (. aspx files), user controls (. ascx files), HTTP handlers (. ashx files), and HTTP modules (. asmx files)

Compile and link to local resource assemblies and top-level assemblies as needed.

Themes, master pages, other source files

Compile the appearance files for each theme, master page, and other source code files that are referenced by those pages when you compile the reference page.

The compiled assembly is cached on the server and reused on subsequent requests, and is retained between application restarts as long as the source code has not changed.

Because the application compiles on the first request, the initial request to the application takes significantly longer than the subsequent request. You can precompile the application to reduce the time that is required for the first request. For more information, see How to: Precompile an ASP.

Application restarts (number of application restarts)

Modifying the source code of the WEB application will cause ASP. NET to recompile the source file into an assembly. When you modify a top-level item in an application, all other assemblies in your application that reference the top-level assembly are also recompiled.

In addition, modifying, adding, or removing certain types of files in the application's well-known folders will cause the application to restart. The following actions will cause the application to restart:

    • Add, modify, or delete an assembly in the application's Bin folder.

    • Add, modify, or delete localized resources in the App_GlobalResources or App_LocalResources folder.

    • Add, modify, or delete an application's Global.asax file.

    • Add, modify, or delete source code files in the App_Code directory.

    • Add, modify, or delete configuration file configurations.

    • Add, modify, or delete Web service references in the App_WebReferences directory.

    • Add, modify, or delete the Web. config file for your application.

When an application requires a restart, ASP. NET will service all pending requests from the existing application domain and from the old assembly before restarting the application domain and loading the new assembly.

HTTP Module

The ASP. NET application life cycle can be extended through the IHttpModule class. ASP. NET contains several classes that implement IHttpModule , such as the SessionStateModule class. You can also create your own classes that implement IHttpModule .

If you add modules to your application, the module itself raises events. By using the modulename_eventname Convention, applications can subscribe to these events in the Global.asax file. For example, to handle the authenticate event raised by the FormsAuthenticationModule object, you can create a handler named formsauthentication_authenticate .

By default, the SessionStateModule class is enabled in ASP. All session events are automatically named session _event, such as Session_Start. The Start event is raised each time a new session is created. For more information, see Session State Overview.

Overview of the ASP. NET application (application) life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.