Tuesday, July 27, 2010

ASP.NET PageLife Cycle along HttpHandlers and HttpModules

A very common argument/assumption is the ignorance of managers w.r.t to technical details or subject matter. Not sure whether the argument needs to be defended or agreed but I would like to throw some light on it from my perspective and then delve into detail of ASP.Net, as promised in the above title.

Most of the times managers are caught up in activities like Planning, Resource estimating and forecasting, budget issues, tracking the plans and various quality parameters, various reports to various stakeholders and various other organizational initiatives (making proposals, recruitment drives, ISMS 27001 audit, appraisals, etc). This leaves the managers with little time to flaunt with any of the technical skills, they may possess. Having said that, to certain extent and in some cases there is ignorance on technical subject matters on behalf of managers which is definately not laudable.

So to bridge the gap of the common opinion/argument and also enhance my understanding on technical matters, I will try and write on my blog about technical subjects, every now and then.

Introduction:

Every request that comes to IIS is routed to the appropriate ISAPI dll. For instance a request for “.ASPX” page is sent to “aspnet_isapi.dll”. Now if this is the first page being called for a particular dot net site/application, then an App Domain is created for the requested site. Creation of App Domains for each site/application restricts the access of the site to its app domain and thus protects other sites/app domain from its failure/errors.

After the creation of App Domain various .Net creates hosting environment HttpRunTime and then te other core ASP.Net objects likeHttpContext, HttpRequest, HttpResponse .

Once the core objects are created then HttpApplication object is created (or picked up from pool) to serve the request. It is then assigned to the core ASP.Net objects to process the pages.

Having the app domain in place and the creation of core ASP.Net objects completed, it is time for execution of events for the requested page/files.

Event cycle

The event cycle in summary is as depicted in the diagram below:



Before the events of page cycle execute, certain generic event handlers like Http Module Events and Http Handler events are executed.

Module events like BeginRequest, AuthenticateRequest, AuthorizeRequest, ResolveRequestCache , AcquireRequestState, PreRequestHandlerExecute, etc are executed first giving the chance for anything generic like Authencation, Authorization to be executed before the actual page starts to execute.

After the Module events, the baton is handed over to any processing of any Handler events that may have been registered. Handlers are registered for one or more file extensions and are hence extension based processors.

After the handler events the actual page events are executed (detailed below).

Once the page events are executed and the page is unloaded from memory, certain post page execution events of HttpModule are executed i.e. 'PostRequestHandlerExecute', 'ReleaserequestState', 'UpdateRequestCache' and 'EndRequest'.

(PostRequestHandlerExecute, i think should be executed after the Handler has executed and not after the end of page life cycle events).

Page Events

Let’s discuss the various Page Events and in the order in which they occur.

Init

Used to create server controls from the received HTML. Server controls canot be accessed here. Can be used to create dynamic controls, if any

LoadView State

View State is applied to the server controls. Can access view state here

PostBackData

Data posted from client is applied to the controls here. Can access form data here

Load

Can use any of the page controls and write desired logic

Validate

Validators are executed here

Event

Execution of any of the events that triggered the post back i.e button click, drop down change, etc

Pre-render

Any final changes to UI objects

SaveViewState

Saving of control data to view state

Render

Rendering of controls. Custom HTML output, if you want to add.

Unload

Any clean that one needs to do

References:

Following are the references used for understanding and presenting the information above

http://www.codeproject.com/KB/aspnet/ASPDOTNETPageLifecycle.aspx

http:// www.15seconds.com/Issue/020417.htm

No comments:

Post a Comment