Global.asax


When you add a new item to your ASP.NET application, you get the Add New Item dialog. From here, you can add a Global Application Class to your applications. This adds a Global.asax file. This file is used by the application to hold application-level events, objects, and variables - all of which are accessible application-wide. Active Server Pages developers had something similar with the Global.asa file.

Your ASP.NET applications can have only a single Global.asax file, which supports a number of items. When it is created, you are given the following template:

  <%@ Application Language="VB" %> <script runat="server">     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)         ' Code that runs on application startup     End Sub     Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)         ' Code that runs on application shutdown     End Sub     Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)         ' Code that runs when an unhandled error occurs     End Sub     Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)         ' Code that runs when a new session is started     End Sub     Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)         ' Code that runs when a session ends.         ' Note: The Session_End event is raised only when the sessionstate mode         ' is set to InProc in the Web.config file. If session mode is         ' set to StateServer         ' or SQLServer, the event is not raised.     End Sub </script> 

Just as you can work with page-level events in your .aspx pages, you can work with overall application events from the Global.asax file. In addition to the events listed in this code example, the following list details some of the events you can structure inside this file:

  • Application_Start: Called when the application receives its very first request. It is an ideal spot in your application to assign any application-level variables or state that must be maintained across all users.

  • Session_Start: Similar to the Application_Start event except that this event is fired when an individual user accesses the application for the first time. For instance, the Application_ Start event fires once when the first request comes in, which gets the application going, but the Session_Start is invoked for each end user who requests something from the application for the first time.

  • Application_BeginRequest: Although it is not listed in the preceding template provided by Visual Studio 2005, the Application BeginRequest event is triggered before each and every request that comes its way. This means that when a request comes into the server, before this request is processed, the Application_BeginRequest is triggered and dealt with before any processing of the request occurs.

  • Application_AuthenticateRequest: This is triggered for each request and enables you to set up custom authentications for a request.

  • Application_Error: Triggered when an error is thrown anywhere in the application by any user of the application. This is an ideal spot to provide application-wide error handling or an event recording the errors to the server’s Event Logs.

  • Session_End: When running in InProc mode, this event is triggered when an end user leaves the application.

  • Application_End: Triggered when the application comes to an end. This is an event that most ASP.NET developers won’t often use because ASP.NET does such a good job of closing and cleaning up any objects that are left around.

In addition to the global application events that the Global.asax file provides access to, you can also use the following directives in this file, just as you can with other ASP.NET pages:

  • @Application

  • @Assembly

  • @Import

These directives perform in the same way when they are used with other ASP.NET page types. An example of using the Global.asax file is shown in the next code example. It demonstrates how to log when the ASP.NET application domain shuts down. When this happens, the ASP.NET application abruptly comes to an end, so place any logging code in the Application_End method of the Global.asax file:

  <%@ Application Language="VB" %> <%@ Import Namespace="System.Reflection" %> <%@ Import Namespace="System.Diagnostics" %> <script runat="server">     Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)         Dim MyRuntime As HttpRuntime = _            GetType(System.Web.HttpRuntime).InvokeMember("_theRuntime", _            BindingFlags.NonPublic Or BindingFlags.Static Or _            BindingFlags.GetField, _            Nothing, Nothing, Nothing)         If (MyRuntime Is Nothing) Then             Return         End If         Dim shutDownMessage As String = _            CType(MyRuntime.GetType().InvokeMember("_shutDownMessage", _            BindingFlags.NonPublic Or BindingFlags.Instance Or            BindingFlags.GetField, _            Nothing, MyRuntime, Nothing), System.String)         Dim shutDownStack As String = _            CType(MyRuntime.GetType().InvokeMember("_shutDownStack", _            BindingFlags.NonPublic Or BindingFlags.Instance Or            BindingFlags.GetField, _            Nothing, MyRuntime, Nothing), System.String)         If (Not EventLog.SourceExists(".NET Runtime")) Then             EventLog.CreateEventSource(".NET Runtime", "Application")         End If         Dim logEntry As EventLog = New EventLog()         logEntry.Source = ".NET Runtime"         logEntry.WriteEntry(String.Format(_            "shutDownMessage={0}\r\n\r\n_shutDownStack={1}", _            shutDownMessage, shutDownStack), EventLogEntryType.Error)     End Sub </script> 

With this code in place in your Global.asax file, start your ASP.NET application. Next, do something to cause the application to restart. For example you could make a change to the web.config file while the application is running. This triggers the Application_End event, resulting in the addition to the Event Log shown in Figure 19-12.

image from book
Figure 19-12




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net