471,079 Members | 949 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,079 software developers and data experts.

HttpApplication.EndRequest Event NOT Calling my Method!

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?
Nov 19 '05 #1
2 8129
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/
----------------------------------
Making waves on the Web
"Breeto" <Br****@discussions.microsoft.com> wrote in message
news:57**********************************@microsof t.com...
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?

Nov 19 '05 #2
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. =\
"Rick Strahl [MVP]" wrote:
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/
----------------------------------
Making waves on the Web
"Breeto" <Br****@discussions.microsoft.com> wrote in message
news:57**********************************@microsof t.com...
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?


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Clarence Gardner | last post: by
3 posts views Thread by AinO | last post: by
1 post views Thread by Fernando Arámburu | last post: by
2 posts views Thread by Urs Eichmann | last post: by
9 posts views Thread by jeff | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.