Sideway
output.to from Sideway


Application and Session Objects


ASP.NET Plafform Application and Session



Draft for Information Only

Content

ASP.NET Application
 Application Class
 Application Instance
 Application Events
  HttpApplication Class
   HttpApplication Event
    IIS 5.0 and 6.0
     IIS 7.0
  Application_OnStart and Application_OnEnd
 Application State
  References

ASP.NET Application

Application Class

An application class is defined in the Global.asax file. The code in Global.asax defines a new class that is derived from System.Web.HttpApplication. In the absence of a Global.asax file, the base class, HttpApplication, is used as the application class.

Application Instance

The ASP.NET runtime creates as many instances of application classes as needed to process requests simultaneously. For most applications, this number is limited to the number of threads and remains in the range of 1 through 100, depending on the hardware, server load, configuration, and so on. Many requests reuse application instances, and a free list of application instances is kept during periods of reduced load. Application instances are used in a thread-safe manner, that is, one request at a time. This has important implications:

  • You do not have to worry about locking when you access non-static members of the application class.
  • Application code can store request data for each request in non-static members of the application class (but not after the EndRequest event because this event may maintain the request for a long time).

Because static members of any class, including an application class, are not thread-safe, the user code must provide appropriate locking for access to static members. This applies to any static member that you add to the application class.

Use the following guidelines to access the application instance that is associated with the current request:

  • From the Global.asax, use the this or Me object.
  • From a page, every page includes a strongly-typed ApplicationInstance property.
  • From the HttpContext object, use the HttpContext.ApplicationInstance property (which you type as HttpApplication).

Because Application is used to refer the global application state in classic ASP, ASP.NET uses ApplicationInstance and not Application as a property name of HttpContext  to refer the application instance of HttpApplication that processing the current request.

Application Events

HttpApplication Class

HttpApplication Event

In ASP.NET, an typical application request consists of a series of the application events

  1. BeginRequest
  2. AuthenticateRequest event
  3. DefaultAuthentication internal event
  4. AuthorizeRequest event
  5. ResolveRequestCache event
  6. Internal step to "map handler" (when compilation takes place, a page instance is created)
  7. AcquireRequestState event
  8. PreRequestHandlerExecute event
  9. Internal step to "execute handler" (when the page code is executed)
  10. PostRequestHandlerExecute event
  11. ReleaseRequestState event
  12. Internal step to filter responses UpdateRequestCache event
  13. UpdateRequestCache event
  14. EndRequest evenEndRequest event

Note: If you use Server.Transfer or Response.Redirect(string), the current request is effectively interrupted and some of the events that are listed above will not be raised. However, the EndRequest event will be raised in this scenario.

The following items can handle these events:

  • Internal ASP.NET page framework (for example, steps 6, 9, and 12 in the preceding list).
  • HTTP modules that are configured for the application. The default list of HTTP modules is defined in the Machine.config file.
  • Code in Global.asax that is hooked through the Application_[On]EventName method or that is hooked explicitly when you add event handlers for an alternative handler name.

Each event can have synchronous and asynchronous subscribers. Asynchronous subscribers are executed first. Not all events are always executed; the only event that is always executed is EndRequest. As a result, perform all after-request cleanup in the EndRequest event.

Note In most cases, the actual response is sent to the client after the application instance is finished with the response (which is after EndRequest).

IIS 5.0 and 6.0

For IIS 5.0 and 6.0, the application events handled by HttpApplication class are

  1. Validate the request, which examines the information sent by the browser and determines whether it contains potentially malicious markup.
  2. Perform URL mapping, if any URLs have been configured in the UrlMappingsSection section of the Web.config file.
  3. Raise the BeginRequest event.
  4. Raise the AuthenticateRequest event.
  5. Raise the PostAuthenticateRequest event.
  6. Raise the AuthorizeRequest event.
  7. Raise the PostAuthorizeRequest event.
  8. Raise the ResolveRequestCache event.
  9. Raise the PostResolveRequestCache event.
  10. Based on the file name extension of the requested resource (mapped in the application's configuration file), select a 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 the page before creating an instance of it.
  11. Raise the PostMapRequestHandler event.
  12. Raise the AcquireRequestState event.
  13. Raise the PostAcquireRequestState event.
  14. Raise the PreRequestHandlerExecute event.
  15. Call the ProcessRequest method (or the asynchronous version IHttpAsyncHandlerBeginProcessRequest) of the appropriate IHttpHandler class for the request. For example, if the request is for a page, the current page instance handles the request.
  16. Raise the PostRequestHandlerExecute event.
  17. Raise the ReleaseRequestState event.
  18. Raise the PostReleaseRequestState event.
  19. Perform response filtering if the Filter property is defined.
  20. Raise the UpdateRequestCache event.
  21. Raise the PostUpdateRequestCache event.
  22. Raise the EndRequest event.
  23. Raise the PreSendRequestHeaders event.
  24. Raise the PreSendRequestContent event.

 IIS 7.0

For IIS 7.0, the application events handled by HttpApplication class are

  1. Validate the request, which examines the information sent by the browser and determines whether it contains potentially malicious markup.
  2. Perform URL mapping, if any URLs have been configured in the UrlMappingsSection section of the Web.config file.
  3. Raise the BeginRequest event.
  4. Raise the AuthenticateRequest event.
  5. Raise the PostAuthenticateRequest event.
  6. Raise the AuthorizeRequest event.
  7. Raise the PostAuthorizeRequest event.
  8.  Raise the ResolveRequestCache event.
  9. Raise the PostResolveRequestCache event.
  10. Raise the MapRequestHandler event. An appropriate handler is selected based on the file-name extension of the requested resource. The handler can be a native-code module such as the IIS 7.0 StaticFileModule or a managed-code module such as the PageHandlerFactory class (which handles .aspx files).
  11. Raise the PostMapRequestHandler event.
  12. Raise the AcquireRequestState event.
  13. Raise the PostAcquireRequestState event.
  14. Raise the PreRequestHandlerExecute event.
  15. Call the ProcessRequest method (or the asynchronous version IHttpAsyncHandlerBeginProcessRequest) of the appropriate IHttpHandler class for the request. For example, if the request is for a page, the current page instance handles the request.
  16. Raise the PostRequestHandlerExecute event.
  17. Raise the ReleaseRequestState event.
  18. Raise the PostReleaseRequestState event.
  19. Perform response filtering if the Filter property is defined.
  20. Raise the UpdateRequestCache event.
  21. Raise the PostUpdateRequestCache event.
  22. Raise the LogRequest event.
  23. Raise the PostLogRequest event.
  24. Raise the EndRequest event.
  25. Raise the PreSendRequestHeaders event.
  26. Raise the PreSendRequestContent event.

Note: The MapReNote: The MapRequestHandler, LogRequest, and PostLogRequest events are supported only if the application is running in Integrated mode in IIS 7.0 and with the .NET Framework 3.0 or later.

Application_OnStart and Application_OnEnd

ASP.NET introduces the unique Application_OnStart and Application_OnEnd "events" for compatibility with classic ASP. These "events" are executed only once in the lifetime of an application and not for every application instance. Therefore, if you change non-static members in these methods, you affect only one application instance and not all instances. You can initialize one application instance either in the constructor or by overriding the Init method.

Application_OnStart is a logical equivalent to the class constructor for the application class, but it offers one advantage: the code has access to the HttpContext for the first request to the application.

Application State

Application state is a global dictionary of late-bound objects, which classic ASP introduces to compensate for the absence of global variables in Microsoft Visual Basic Scripting Edition (VBScript). In ASP.NET, you can access application state through one of the following:

  • Application property (which is defined in both HttpApplication and Page classes)
  • HttpContext.Application

ASP.NET includes application state primarily for compatibility with classic ASP so that it is easier to migrate existing applications to ASP.NET. It is recommended that you store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary.

To access static application members from pages in Microsoft Visual C# .NET and Microsoft Visual Basic .NET, you must use the ClassName attribute in Global.asax to name your application class. For example:

If a static member is named MyStaticMember in your Global.asax file, you can use MyClass.MyStaticMember to access it from your page. <%@ Application Language="C# | VB" ClassName="MyClass" %>

References

  • https://support.microsoft.com/en-us/help/312607/info-application-instances-application-events-and-application-state-in
  • https://msdn.microsoft.com/en-us/windows/desktop/ms178473
  • https://msdn.microsoft.com/en-us/windows/desktop/bb470252
  • https://docs.microsoft.com/en-us/previous-versions/75x4ha6s%28v%3dvs.140%29

©sideway
close

References

  1. Active Server Pages, http://msdn.microsoft.com/en-us/library/aa286483.aspx, updated 20Dec2012
  2. ASP Overview, http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx, updated 21Dec2012
  3. ASP Best Practices, http://technet.microsoft.com/en-us/library/cc939157.aspx, updated 15Apr2013
  4. ASP Built-in Objects, http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx, updated 20Dec2012
  5. Response Object, http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx, updated 20Dec2012
  6. Request Object, http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx, updated 20Dec2012
  7. Server Object (IIS), http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx, updated 20Dec2012
  8. Application Object (IIS), http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx, updated 20Dec2012
  9. Session Object (IIS), http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx, updated 20Dec2012
  10. ASPError Object, http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx, updated 20Dec2012
  11. ObjectContext Object (IIS), http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx, updated 20Dec2012
  12. Debugging Global.asa Files, http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx, updated 15Apr2013
  13. How to: Debug Global.asa files, http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx, updated 15Apr2013
  14. Calling COM Components from ASP Pages, http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx, updated 15Apr2013
  15. IIS ASP Scripting Reference, http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx, updated 15Apr2013
  16. ASP Keywords, http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx, updated 15Apr2013
  17. Creating Simple ASP Pages, http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx, updated 15Apr2013
  18. Including Files in ASP Applications, http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx, updated 15Apr2013
  19. ASP Overview, http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx, updated 15Apr2013
  20. FileSystemObject Object, http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx, updated 14Mayr2013
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx, ADO Object Model, updated 15Apr2013
  22. ADO Fundamentals, http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx, updated 14Mayr2013
close

ID: 190600011 Last Updated: 2019/6/11 Revision: Ref:

IMAGE

Home (5)

Business

Management

HBR (3)

Information

Recreation

Hobbies (7)

Culture

Chinese (1097)

English (336)

Reference (66)

Computer

Hardware (149)

Software

Application (187)

Digitization (24)

Numeric (19)

Programming

Web (618)new

CSS (SC)

ASP.NET (SC)

HTML

Knowledge Base

Common Color (SC)

Html Entity (Unicode) (SC)

Html 401 Special (SC)

OS (388)new

MS Windows

Windows10 (SC)

.NET Framework (SC)

DeskTop (7)

Knowledge

Mathematics

Formulas (8)

Number Theory (206)

Algebra (20)

Trigonometry (18)

Geometry (18)

Calculus (67)

Complex Analysis (21)

Engineering

Tables (8)

Mechanical

Mechanics (1)

Rigid Bodies

Statics (92)

Dynamics (37)

Fluid (5)

Fluid Kinematics (5)

Control

Process Control (1)

Acoustics (19)

FiniteElement (2)

Biology (1)

Geography (1)


Copyright © 2000-2019 Sideway . All rights reserved Disclaimers last modified on 10 Feb 2019