MenuBar

Tuesday 15 July 2014

Asp.net page life cycle (the internal of request and response process in asp.net)

As you all know that request and response process is very common, but for any non developer or user’s point of view it’s just a process of calling or requesting for a page through typing the name of page or clicking on a link of page, the requested page will gets found and will display on user’s browser as simple as that.
But the internal process of request and response is far complicated then it looks like. As a web developer it is very important to understand the internal working process of request and response, what happened when user send request to server, how web server get the request and then response back to user.
So in this post I will try to explain the internal process of page request and response and try to clear your concept on asp.net page life cycle.

In asp.net the process of a request for asp.net page from user’s browser, then request acquired by web server and then server sends response back to the user’s browser. In between these processes IIS (Internet Information Services) performs and maintains collection of stuff.

Small Intro of IIS (Internet Information Services) 

IIS is a web server software, which waits for browser requests and serves the appropriate pages.
In a simple word, when you want your website to be publically accessible through the internet, you need to publish it on IIS web server (in case of ASP.NET).

A variety of colors of IIS

• Windows XP Professional comes with IIS 5.1. (Previous version or other editions of Windows XP don’t include)
• Windows Server 2003 comes with IIS 6.
• Windows Vista and Windows Server 2008 come with IIS 7.

IIS request and response phenomenon 

Initial handling is actually performed by the kernel-mode HTTP driver http.sys.When user sends a request to IIS,request goes through ISAPI filter . ISAPI.dll checks requested file extension and performs further action accordingly in the case of .aspx page then it sends to ASPNET_ISAPI.dll extension to process request. As explained in picture below:



IIS request handling

IIS checks whether it is the first request for the website or not then a class called ‘ApplicationManager’ creates an application domain where the website can run. The application domain is responsible for maintain isolation between two or multiple  web sites hosted on the same IIS web server. After that http runtime object created to create hosting environment for all aspx page. Once the hosting environment is created, the compulsory core  ASP.NET core objects (HttpContext , HttpRequest, HttpResponse). After that main object is created which is responsible to serve the .aspx page is called HttpApplication. In case if website has global.asax file available then the object of global.aspx class is created.
HttpApplication object has the command of all core object of asp.net that created earlier. Now HttpApplication of object process the request by calling HttpModules, HttpHandlers and Page events.

These three objects invokes different events that can be  used by developer to insert  logic within that as required.


HttpHandler :

HttpHandler is an extension based processor that means it checks the extension of requested file and then calls appropriate handler. To get more clear see the image below :




HttpHandler

HttpModule :

HttpModule is an event based processor, that means we can insert code in ASP.NET pipeline events. ASP.NET pipeline events are listed in the order that they occur:





Description of all above Event below :


BeginRequest
This event indicates that a new request occurred  and guarantees to be raised on each page request.

AuthenticateRequest
This event indicates that ASP.NET runtime is ready to authenticate the user and any authentication code can be injected here as required.

AuthorizeRequest
This event indicates that ASP.NET runtime is ready to authorize the user and Any authorization code can be injected here as required.

ResolveRequestCache
In this event, ASP.NET runtime take decision whether the page can be served from the cache rather than loading the original for increase the performance. Any caching specific code can be injected here.

AcquireRequestState
This event indicates that ASP.NET runtime is ready to acquire session variables.

PreRequestHandlerExecute
This event is indicates that to handling control to the HttpHandler can be done and required code can be injected.           

ProcessRequest
In the case of HttpHandler defined in page ,Httphandler logic is can be written logic which needs to be executed as per page extensions.

Page  Init
This event raised just before page load and code like Creation of controls dynamically, Any initialization  etc.

Load
This Event guarantees that ASP.NET controls are fully Loaded and you write code logic as required.

Validate
If you have any validation controls  on your page, you can check whether validated or not and write code as required.


Render
When request processed by the server then it need to be send back to user’s browser rendered as simple html code, so before rendering you can insert final code to change something you want.

Unload
This event is called when Page object is going to unloaded and released all created objects from the memory.

PostRequestHandlerExecute
This event occurs after the handlers are executed successfully any code logic can be injected here.

ReleaserequestState
In this event all saved state for page gets released you can save state
variables like session variables if you need.

UpdateRequestCache
In this event you can update your cache if you need.

EndRequest
The End of the Request.
I hope so that this post help you to understand the lifecycle of ASP.NET Page.



Video Reference 



2 comments: