HttpApplication EndRequest Event Invoked Many Times In Single Request?

By | December 14, 2008

The other day I was putting the last touch of a temporary way to manage the NHibernate session (ISession) in Bunian. So part of the task was to bind a method to the HttpApplication EndRequest event (in the Global.asax.cs file) like the following:

public override void Init()


       this.EndRequest += WorkContext.NHibernateSessionManager.Instance.HttpRequestEnded;


By doing this, at the end of each page request the NHibernateSessionManager.Instance.HttpRequestEnded() will be called and I can clean the session then. But to my surprise this method was called at least 10 times!! So I thought maybe the Global.Init() method is called many times for some reason and I ended up binding the same method to the EndRequest event many times, so I set a breakpoint at the Global.Init() method and…it’s called one time only.

That was strange, ok so it’s only the HttpRequestEnded() mehtod that is called many times, but I am requesting only one page!! how come there are 10 requests!

So I opened Firefox which is already “armed” with the magnificent add-on Firebug, and I requested the page again, Firebug showed the following:


And that was it!! the page contained 10 resources (1 CSS file and 9 images), OH! so it is one page, but for each resource referenced on the page you get a request, hence a raise of the EndRequest event.

I couldn’t love Firebug more; I am not only happy that I wasn’t doing something wrong, but yet I learned something new about the ASP.NET internals. awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *