Application Domain provides isolation between different
applications for global variables and allows each application to be unloaded
separately.
Multiple Application Domains can run in a Single Process.
Why Isolation?
Single process can have multiple threads, memory leak, out-of-bound
memory access, null object referencing, etc. If one of the applications crashes
because of these, then all the application crashes; therefore, Isolation of
application is required.
HostingEnvironment
An instance of a class named HostingEnvironment is created
which resides in Application Domain.
Once the Application Domain is created and
HostingEnvironment object is instantiated, Http requests are passed to an
instance of the HttpRuntime class. HttpRuntime object examines the request and
figures out which application it was sent to (i.e. virtual directory). It the
uses HttpApplicationFactory to either find or create an HttpApplication object
to process the request.
ASP.NET creates and initializes the core objects such as
HttpContext, HttpRequest and HttpResponse.
After all the core objects are initialized, the application
is started by creating an instance of the HttpApplication class.
HttpApplication holds a collection of Http module objects
Application is started by creating an instance of the HttpApplication
class. If the application contains Global.asax, it overrides the Global.asax
file, which is derived from the HttpApplication.
The first time an ASP.NET page or process is requested in an
application, a new instance of HttpApplication is created. To maximize
performance, HttpApplication instances might be reused for multiple requests.
An instance of HttpApplication processes only one request at
a time.
The following are the events which are called during the
application life cycle.
1. Application_Start: It is called when the first resource
in an ASP.NET application is requested.
2. Application_event: It is called at the appropriate time
during the application life cycle.
Some of the Application events are as follows.
Application_BeginRequest: Whenever ASP.NET responds to the
Request, it will be the first to occur in the Http Pipeline.
Handler
Listing 1
public event EventHandler BeginRequest;
Application_AuthenticateRequest: Occurs whenever the
identity of the user has to be established.
Handler
Listing 2
public event EventHandler AuthenticateRequest;
Application_AuthorizeRequest: Occurs whenever the identity
of the user has been verified.
Handler
Listing 3
public event EventHandler AuthorizeRequest;
Application_Error: Occurs when ever the unhandled exception
is thrown.
Handler
Listing 4
public event EventHandler Error;
Application_EndRequest: Whenever ASP.NET responds to the
Request, it will be the last to occur in the Http Pipeline chain.
Handler
Listing 5
public event EventHandler EndRequest;
3. HttpApplication.Init: called once for every instance of
the HttpApplication class.
4. Application_End: Called once per lifetime of the
application before the application is unloaded.