A walkthrough to Application State

A walkthrough to Application State

As we all know,web is stateless .A Web page is recreated every time it is posted back to the server.In traditional web programming, that all the information within the page and control gets wiped off on every postback.To overcome this problem,ASP.NET framework provides various ways to preserve the states at various stages like controlstate,viewstate,cookies,session etc.These can be defined in at client side and server side state management.Please see the image below.

Various ways to maintain the state
Application LifeCycle
First I am going to explain ASP.NET Application Lifecycle.One need to really understand the application Lifecycle, so that one code efficiently and use the resources available.Also it is very important to discuss, as we are going to Application level events, objects etc.

ASP.NET uses lazy initialization technique for creating the application domains ie Application domain for an application is created only when the first request is recieved by the web server. We can categorise Application life cycle in several stages.These can be

Stage 1: User first requests any resource from the webserver.

Stage 2: Application recieves very first request from the application.

Stage 3: Application basic Objects are created

Stage 4: An HTTPapplication object is assigned to the request.

Stage 5: And the request is processed by the HTTPApplication pipeline

I’ll explain the points one by one.

Stage 1: The Application life cycle starts when a user hits the URL by typing it to on browser.The browser sent this request to the webserver.When webserver recieves the request from browser, it examines the file extension of the requested file and checks which ISAPI extension is required to handle this request and then passes the request to approriate ISAPI extension.
Application state flow

Note 1: If any extension is not mapped to any ISAPI extension, then ASP.NET will not recieve the request and request is handled by the server itself and ASP.NET authentication etc.. will not be applied.

Note 2: We can also make our own custom handler, to process any specific file extension.

Stage 2: When ASP.NET recieves first request, the Application manager creates an application domain for it,Application domain are very important because they provide the isolation amongst various applications on the webserver and every application domain is loaded and unloaded separately and in application domain an instance of class HostingEnvironment is created which provides access to information about all application resources.
Application Lifecycle

Stage 3: After creating the application domain and hosting environment, asp.net initializes the baisc objects as HTTPContext, HTTPRequest and HTTPResponse. HTTPContext holds objects to the specific application request as HTTPRequest and HTTPResponse.HTTPRequest contains all the informaion regarding the current request like cookies, browser information etc. and HTTPResponse contains the response that is sent to client.

Stage 4: Here all the basic objects are being initialized and the application is being started with creation of HTTPApplication class, if there is Global.asax(It is derived from HTTPApplication class) is there in the application then that is instantiated.
Application Life Cycle

Note: When first time the application is accessed the HTTPApplication instance is created for further reqests it may be used other requests as well.

Stage 5
: There are a lot of events are executed by the HTTPApplication class.Here I have listed down few important ones.These events can be used for any specific requirement.

Page Life Cycle

Complete article has been published on CodeProject. To view Click here