ASP.NET Application Life Cycle
Have you ever wondered about all the stages that an ASP.NET request goes through? Ever wonder why it is such a performance hit to have a wildcard mapping to map all extensions on your web server to ASP.NET? This information corresponds to IIS 5.0 and 6.0. For information on the life cycle in IIS 7.0, see ASP.NET Application Life Cycle Overview for IIS 7.0. You can also check out the ASP.NET Page Life Cycle Overview for information on what happens once a page is run.
User requests a resource
A request comes into the web server and it will check to see what is mapped to handle the request. ASP.NET is an ISAPI extension under the web server.
ASP.NET receives the first request for an application
ASP.NET will first create the application domain for this application. These allow each application to be isolated from each other. ASP.NET then compiles all the top-level items in the application, if required, including the App_Code folder.
ASP.NET core objects created for the request
ASP.NET will create the Request, Response, and Context objects for the request. This includes browser information, cookies, headers and everything that the server will respond back to the client with.
HttpApplication is assigned to the request
The application is started by creating the HttpApplication object, taking into account the Global.asax file if it exists. At this stage, ASP.NET will create any configured modules, such as SessionStateModule to handle Session.
HttpApplication pipeline
The following events occur:
- Validate the request – checking for malicious markup
- Perform URL mapping
- BeginRequest event
- AuthenticateRequest event
- PostAuthenticateRequest event
- AuthorizeRequest event
- PostAuthorizeRequest event
- ResolveRequestCache event
- PostResolveRequestCache event
- Find the requested resource, if it is a Page, compile the page
- PostMapRequestHandler event
- AcquireRequestState event
- PostAcquireRequestState event
- PreRequestHandlerExecute event
- Call ProcessRequest on the HttpHandler
- PostRequestHandlerExecute event
- ReleaseRequestState event
- PostReleaseRequestState event
- Response filtering
- UpdateRequestCache event
- PostUpdateRequestCache event
- EndRequest event
- PreSendRequestHeaders event
- PreSendRequestContent event
Hopefully this will give you a little perspective on all of the different times that you can hook into a request and the different things you can act on. There are additional things you can do, such as Application_Start events in the global.asax file. For those and some nice pictures of some of the above data, take a look at the MSDN page here.
Comments
Anonymous
May 27, 2008
PingBack from http://blogs.msdn.com/tom/archive/2008/05/27/asp-net-application-life-cycle.aspxAnonymous
May 27, 2008
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
May 27, 2008
Hi, I really appreciate that you have explained the entire things in very simple words. Now just to carry on the discussion, can you please explain these things
- What are the things which causes an application to restart ? Say change in connection string in Web.Config or Change in Web.Config file
- What makes the entire worker process restarted ?
- How IIS can be configured to deliver best performance My intention is to make a complete knowledge base, where everything related to ASP.NET infrastructure is available. Thank You, Shail
- Anonymous
May 28, 2008
Shail, Thanks for the comment. Here are my answers:
- Numerous things can cause it to restart. Any changes to any of the files the application depends on will cause it to restart. The config files are just a few of them
- Crashes can cause the process to restart, or if it hits one of the health monitoring limits.
- Configuring IIS really depends on what you are doing with it, take a look at this post for some places to start: http://blogs.msdn.com/tom/archive/2008/03/25/hangs-and-how-to-solve-them-part-2-queuing.aspx Keep the questions coming...
Anonymous
May 28, 2008
The comment has been removedAnonymous
May 29, 2008
The comment has been removedAnonymous
March 22, 2010
Good Post Tom, i really appreciate even Shail's queries are also nice. Can you give some idea to remove javascript error from a web page (very common error in Asp.net web development)using IE8 or IE7.Anonymous
March 24, 2010
Subi, Typically that is caused by WebResource.axd problems. One such problem is discussed http://blogs.msdn.com/tom/archive/2010/02/16/intermittently-the-server-sends-back-0-byte-webresource-axd-and-scriptresource-axd-files.aspx The problem really depends on what is causing the javascript error. If it isn't WebResource then you need to see what the error is and determine what can be done to fix it.Anonymous
April 13, 2010
We have a ASP.Net web application. I have been told that the initial request takes too long to bring up our Launch Page. I added trace output all along the HttpApplication pipeline lifecycle. I noticed that there is a 7 second (average) delay between the PostResolveRequestCache event and PostMapRequestHandler event. The pipeline lifecycle above states that between these events the application will "10. Find the requested resource, if it is a Page, compile the page ". Is there a way I can precompile the pages so this step can be faster? I have tried using the Publish... option in VS but this still seemed to have this delay. I also placed a .htm page in this web application. The htm page loads the Launch Page into an IFRAME. On the initial request to the application there is a 10 second delay between the htm page displaying in the browser and the Application_Start event. Is there anyway to speed this up? After the initial request to the application has been handled then all subsequent requests do not experience these delays. Any ideas or suggestions you have would be greatly appreciated. Cheers, Scott