HttpApplication.EndRequest Event NOT Calling my Method!

G

Guest

Can anyone please tell me why the following doesn't work...



using System;
using System.Web;

namespace AspTests
{

public class Test : System.Web.UI.Page
{

override protected void OnInit(EventArgs e)
{
System.Diagnostics.Trace.WriteLine( "Test.OnInit()" );
HttpContext.Current.ApplicationInstance.EndRequest += new
EventHandler(ApplicationInstance_EndRequest);
base.OnInit(e);
}

private void ApplicationInstance_EndRequest(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(
"Test.ApplicationInstance_EndRequest()" );
}

}

}


When I run that code my ApplicationInstance_EndRequest is never called.
However, when I modify the Global.asax to include the following code...


protected void Application_EndRequest(Object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(
"Global.ApplicationInstance_EndRequest()" );
}



That code is called! Any clue why the HttpApplication won't fire my event
handler but it will fire the one defined in Global.asax, which I can only
assume is wired the same way I did mine?
 
R

Rick Strahl [MVP]

Hi Breeto,

This is probably not how you should set up an Application level handler like
EndRequest. EndRequest usually is set up in the global.asax, because it is
an HttpApplication event and thus fires for all incoming requests.

What's happening in your case most likely is that you are assigning the the
handler to a method in your Page class - by the time EndRequest fires, the
page is long gone so the handler is invalid/null and won't be called.

You'll want to move that EndRequest handler into global.asax or into an
ASP.NET HTTP Filter.

Note also that EndRequest will not fire if you call Server.Transfer or
Response.End both of which bypass the rest of the HTTP pipeline.

Regards,

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
 
G

Guest

Hey Rick, thanks very much for taking the time to respond.

For my particular situation I actually DO want this code to execute on every
single page request. You might be right that using the global.asax is better
than trying to wire my own even handler to EndRequest. I might move my code
to the Application_EndRequest method in global.asax, but regardless, when
something is not working the way I expect it to work, I feel obligated to
find out why that's the case. =)

In the code sample that I posted I was in fact wiring the EndRequest event
to an instance method in a web page. In my actual application though, where
I encountered the problem, the method I am wiring is static. You can also
change the method in my code sample in this post to static and it still does
not work, so I don't think it's due to a null reference.

Also, your comment about EndRequest not firing after a Server.Transfer or
Response.End got me a little concerned because the code I'm trying to wire to
the HttpApplication.EndRequest event is clean up code that I really need to
happen on every page hit. So I tested this out and it appears that the
Application_EndRequest method does still get called after a
Response.Redirect, Response.End, or a Server.Transfer. And this is a GOOD
thing! =)

Thanks again, but I still want to know why EndRequest won't execute my event
handler. =\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top