ASP.NET 2.0: AcquireRequestState fires multiple times
PROBLEM/TROUBLESHOOTING
=========================
I came across this problem while troubleshooting a "Session Loss" issue. Customer in this case was using the Application_AcquireRequestState method and within this method was trying to use the HttpContext.Current.Session object to do something with it. This was resulting in a NullReferenceException.
While debugging we discovered that the AcquireRequestState method was being called multiple times and the first time it showed up as a valid object, the second time it hit that breakpoint, the value returned was null.
To troubleshoot this issue, i first created a simple sample application which has nothing but a default.aspx page and added the AcquireRequestState event to the Global.asax codefile. The result was that the event was fired only Once as i would expect it.
This would normally indicate that there are multiple resources being requested for in the same page. So i looked at the code of the page and found out that the customer was using some Validation Controls in the page.
This pointed me in the right direction, since i know that in ASP.NET 2.0 we have the WebResource.axd that gets called to retrieve an embedded resource (in this case the WebUIValidation.js from System.Web.dll) which would be retrieved as a separate request. If you look at the Fiddler logs you will see what i am talking about.
RESOLUTION
===========
To workaround this issue we needed to add the following code in the AcquireRequestState event.
if (HttpContext.Current.Request.Url.AbsoluteUri.Contains(".axd"))
{
return;
}