Application Class

The Composite Web Application Block includes the class named WebClientApplication. This class is an implementation of an ASP.NET application class and is the entry point of the Web client application. (This is a class that replaces the default ASP.NET application class.)

Background: ASP.NET and Application Classes

You use the Global.asax file in a Web site to define the code-behind class for the application class. In the following code example, the Global.asax file defines WebClientApplication as the application class.

<%@ Application Language="C#" Inherits="CompositeWeb.WebClientApplication" %> 

ASP.NET starts the application 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 that is derived from the HttpApplication class and uses the derived class to represent the application.

Global.asax is an optional file for ASP.NET applications, but it is required for applications that you create with the Web Client Software Factory.

When an incoming request arrives, the ASP.NET runtime associates the request with an instance of the HttpApplication class. The first time an ASP.NET page or process is requested in an application, a new instance of HttpApplication is created. However, to maximize performance, ASP.NET maintains a pool of HttpApplication objects and can reuse them for multiple requests.
An instance of HttpApplication processes only one request at a time. This simplifies application event handling because you do not have to lock non-static members in the application class when you access them. This also allows you to store request-specific data in non-static members of the application class.
The lifetime of a request consists of a series of the application events. ASP.NET automatically binds application events to handlers in the WebClientApplication class using the naming convention Application_Event, such as Application_BeginRequest.

WebClientApplication HTTPApplication Class

Figure 1 illustrates the classes and interfaces related to the WebClientApplication class.
Figure 1
WebClientApplication application class.

The WebClientApplication uses application events to provide application initialization and Web page processing.

Application Initialization

ASP.NET executes the Application_OnStart and Application_OnEnd events only once in the lifetime of an application and not for every application instance. Application_OnStart is a logical equivalent to the class constructor for the application class. Figure 2 illustrates this process.

If you change non-static members in these methods, you affect only one application instance, not all instances.

Figure 2
ASP.NET events execution.

The Application_Start method of the WebClientApplicationModule class contains the code that uses the following sequence of activities to perform application initialization:
  1. Create Application Builder. This activity creates an instance of an ObjectBuilder Builder class that creates singleton objects. The WebClientApplication class uses this Builder object to construct composition container instances.
  2. CreatePageBuilder. This activity creates an instance of an ObjectBuilder Builder class that does not create singleton objects. The WebClientApplication class uses this Builder object to construct objects and inject dependencies on each Web page request.
  3. AddBuilderStrategies. ObjectBuilder provides an extensible framework that you can use to insert custom code (a strategy) that executes when objects are constructed. This activity adds the application block custom strategies (ones that are useful in a Web application) to the Builder object.
  4. CreateRootContainer. Every application has a single root composition container (see Composition Containers). This activity creates and initializes the root composition container.
  5. AddRequiredServices. In this activity, the application class creates instances of required global application services.
  6. LoadModules. In this activity, the application class loads the application modules (see Modules).
  7. ConfigureModules. In this activity, the application class calls each application module for initialization.
  8. Start. An application can use this activity to peform custom initialization during application startup.

Web Page Processing

The WebClientApplication class executes code to create the objects required by each Web page (.aspx) request. To do this, it handles the following events:
  • Application_PreRequestHandlerExecute. This event occurs just before ASP.NET begins executing an event handler (for example, a page or an XML Web service). In this event, an ObjectBuilder Builder object creates instances of objects and injects dependencies for Web page requests. For example, it will create a presenter object for a view that uses the CreateNew attribute to specify its presenter.
  • Application_PostRequestHandlerExecute. This event occurs when the ASP.NET event handler (for example, a page or an XML Web service) finishes execution. In this event, the TearDown method of the Builder object calls the Builder strategies to allow them to perform appropriate cleanup tasks.
Figure 3 illustrates a page request that is routed to the composition container of the business module that contains that Web page.
Figure 3
Creation and disposal of objects required by a Web page.

Last edited Nov 20, 2007 at 11:52 AM by ejadib, version 2


No comments yet.