Export (0) Print
Expand All

Working with HttpApplication Instances

.NET Framework 1.1

During the lifetime of your application, ASP.NET maintains a pool of Global.asax-derived HttpApplication instances. When your application receives an HTTP request, the ASP.NET page framework assigns one of these instances to process that request. That particular HttpApplication instance is responsible for managing the entire lifetime of the request it is assigned to, and the instance can only be reused after the request has been completed.

It is through the HttpApplication class that you have programmatic access to the HttpApplication.Init and HttpApplication.Dispose methods, as well as the Application_OnStart and Application_OnEnd events. You also have access to any events exposed by any HttpModule.

Overriding the Init and Dispose Methods

You invoke the HttpApplication.Init method immediately after you create an instance of the HttpApplication class. You can use this method to create and configure any object that you want to use across all handling events, as shown in the following example.

<script language="VB" runat="server">
      Public Overrides Sub Init()
         'Insert something interesting here.
      End Sub
</script>
[C#]
<script language="C#" runat="server">
      public override void Init() {
         // Insert something interesting here.
      }
</script>

The Init method is different from the Application_OnStart event, because it is always invoked on all HttpApplication instances within an application. Application_OnStart is raised only once during an application's lifetime, on the first instance of HttpApplication. Use Application_OnStart only to create or modify states shared by all pipeline instances, such as the use of the ApplicationState object. Do not use it to create local variables, because local variables are not shared by multiple HttpApplication instances. Invoke the HttpApplication.Dispose method immediately before destroying an instance of the HttpApplication class. You can use it to clean up any local resources.

The Dispose method is different from the Application_OnEnd event because it is always invoked on all HttpApplication instances within an application. Application_OnEnd is raised only once during an application's lifetime, on the last instance of HttpApplication that is torn down. Use Application_OnEnd only to clean up states or resources shared by all pipeline instances, such as the use of the ApplicationState object. Do not use it to clean up local variables, because local variables are not shared by multiple HttpApplication instances.

You cannot use the Request, Response, and Session properties of an HttpApplication instance during either the Init or the Dispose stage of request execution.

The following example demonstrates how you can override the two life cycle methods provided by the HttpApplication base class.

<script language="VB" runat=server>
   Public Overrides Sub Init()
      ' Init override code goes here.
   End Sub
   Public Overrides Sub Dispose()
      ' Init override code goes here.
   End Sub
</script>
[C#]
<script language="C#" runat=server>
   public override void Init() {
      // Init override code goes here.
   }
   public override void Dispose() {
      // Init override code goes here.
   }
</script>

Handling HttpApplication Events

You can use the Global.asax file to synchronize any event that is exposed by the HttpApplication base class. To do this, you must use the following naming pattern to author methods:

Application_EventName(AppropriateEventArgumentSignature)

For example, if you want code that responds to the OnStart, BeginRequest, and OnEnd events of your application, the code you include in the Global.asax file might look something like the following.

<Script language="VB" runat="server">
     Sub Application_OnStart()
         'Application start-up code goes here.
     End Sub
     Sub Application_BeginRequest()
         'Application code for each request could go here.
     End Sub
     Sub Application_OnEnd()
         'Application clean-up code goes here.
     End Sub
</script>
[C#]
<Script language="C#" runat="server">
     public void Application_OnStart() {
         // Application start-up code goes here.
     }
     public void Application_BeginRequest() {
         // Application code for each request could go here.
     }
     public void Application_OnEnd() {
         // Application clean-up code goes here.
     }
</script>

See Also

Global.asax File | HttpApplication Class | HttpApplication.Init | HttpApplication.Dispose

Show:
© 2014 Microsoft