Daniel James Bryars
Daniel James Bryars

Reputation: 4621

Does Page Unload get called after the response has left IIS?

I'm doing some diagnostic logging in the Page_Unload event in an asp.net application, this logging can take a fair bit of time (about 100ms). Will the response stream get held up by the code in the Page Unload handler? I could do my work asynchronously by using the theadpool but I'd rather not if it won't affect the client's response time.

More information:

@thorkia is correct in that the documentation says that Page_Unload is called after the response is sent to the client, but in my testing (as advised by @steve) it does block. I've tried Casini, IIS Express, Full IIS 7.5 (on a test server) with both release and debug builds, with and without a debugger attached. And, grasping at straws, I tried putting Async=true in the Page Directive. I tried with Fiddler (streaming enabled), and without Fiddler. I've tried with IE9, and Firefox. If the documentation is "correct" then I wonder it it does send the response but perhaps doesn't "finish it off" (what ever that means I'll need to check the HTTP spec) and so the page doesn't render in the browser? But my understanding was that a client browser starts to render the page as it receives the bytes to this doesn't make sense to me either. I've also tried looking at the code in IL Spy but I think this might take me a lot of time.

Now I'm intrigued; am I doing something wrong, or is the documentation misleading?

Upvotes: 4

Views: 3341

Answers (2)

thorkia
thorkia

Reputation: 2002

According to MSDN (http://msdn.microsoft.com/en-us/library/ms178472.aspx) the Page Unload stage is only called after the data has been sent to the client.

Taking a long time to do your logging and clean up will not affect the clients response time for that request but could affect future requests if lots of pages are waiting to be unloaded.

Upvotes: 2

Steve Wellens
Steve Wellens

Reputation: 20640

Why not try it?

protected void Page_UnLoad(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("In Page_UnLoad");
    System.Threading.Thread.Sleep(10000);
    System.Diagnostics.Debug.WriteLine("Leaving Page_UnLoad");
}

Upvotes: 3

Related Questions