Wednesday 12 February 2014

Looking into Application State (ASP.NET)

Introduction

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, 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 client side and server side state management. Please see the image below:
Various option to maintain the states
Figure: Options available to maintain the state
There are lot more written about most of them. In this article, I am going to explore mainly Application state,Application events and Application Objects.

Application LifeCycle

So, as first ASP.NET Application Lifecycle. One needs to really understand the application Lifecycle, so that one can 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, i.e., Application domain for an application is created only when the first request is received 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 receives 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 in the browser. The browser sends this request to the webserver. When webserver receives the request from the 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 the appropriate ISAPI extension.
URL Request Flow
Figure: URL Processing
Note 1: If any extension is not mapped to any ISAPI extension, then ASP.NET will not receive the request and the 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 receives the first request, the Application manager creates an application domain for it. Application domains 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.
ALC1.JPG
Figure: ASP.NET Handling first request
Stage 3: After creating the application domain and hosting environment, ASP.NET initializes the basic objects asHTTPContext, HTTPRequest and HTTPResponse. HTTPContext holds objects to the specific application request asHTTPRequest and HTTPResponse.HTTPRequest contains all the information 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 the creation ofHTTPApplication class, if there is Global.asax (It is derived from HTTPApplication class) in the application, then that is instantiated.
Multiple request processing
Figure: Multiple requests processed by ASP.NET
Note: When the application is accessed for the first time, the HTTPApplication instance is created for further requests. It may be used for other requests as well.
Stage 5: There are a lot of events executed by the HTTPApplication class. Here, I have listed down a few important ones. These events can be used for any specific requirement.
Application Events
Fig: Application Events

No comments:

Post a Comment

The Future of Remote Work, According to Startups

  The Future of Remote Work, According to Startups No matter where in the world you log in from—Silicon Valley, London, and beyond—COVID-19 ...