AcquireRequestState fires 7x's in debug RRS feed

  • Question

  • User921933794 posted

    I have read a couple of other post here with a similar type of issue but there never was an outcome. Everyone though the poster's were crazy it seemed. So, that being said, let me join the crazies...

    In an HttpModule I add an event handler like so;

    Public Sub Init(ByVal app As HttpApplication) Implements System.Web.IHttpModule.Init
    ' Register for pipeline events
    AddHandler app.AcquireRequestState, AddressOf Me
    End Sub

    Private Sub OnAcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
         Dim app As HttpApplication = CType(sender, HttpApplication)
    Dim ctx As HttpContext = app.Context

    ctx.Session(LastRefreshTicketEnTry) Is Nothing Then
    ctx.Session(LastRefreshTicketEnTry) = 0
    End If

    End Sub

    Why would the OnAquireRequestState method fire 2 times when debuggin in an HttpModule. The  module errors the second time when calling the HttpContext.Session object with a NullReferenceException here;

    If ctx.Session(LastRefreshTicketEnTry) Is Nothing Then

     The Content Page (aspx) inherits from a base page which adds 2 event handlers (this is closer to the core of my issue I feel);

    ' Register the PreInit
    AddHandler PreInit, AddressOf BasePage_PreInit
    ' Register a PreRender handler
    AddHandler PreRender, AddressOf RefreshPage_PreRender

    When I implement a Master Page into the mix, OnAquireRequestState is fired 7 times. Once again erroring all times with NullReferenceException when calling HttpContext.Session object.

    So, without the master page and base page, it works, fires once with success. Base Page fires twice, base page with master page fires seven times...

    It is most certainly a very basic concept on page event execution that I am missing here, and my discription, with my record, is vague, but all being said do any lights go off in anyone's head here, because I feel like I am in the dark of night in the Amazon?

    Many thanks for any insight, or slaps to the back of the head as well! :)

    Wednesday, April 19, 2006 1:26 PM

All replies

  • User-1634877164 posted

    Hi Jeff,

    Are you running the application using the built-in VS 2005 Web Server?  If so, then you are probably seeing this called multiple times because the static file resources (CSS files and images) are processed by the built-in web-server -- in which case they will each fire them as well.

    Hope this helps,


    Thursday, April 20, 2006 1:07 AM
  • User921933794 posted


    Let me first say thanks for taking the time out to answer my post. Let it be known that I feel special that "ScottGu" communicated with me! Love the work you do (read your blog regularly and I am honored! Keep up the great writting! ;)

    That does help because I am running in debug mode in VS 2k5 within the built in web server.
    I changed the code to handle the Null Ref in the AquireRequestState event handler and it seems to work around the problem;

    ' Get access to the HTTP context
    Dim app As HttpApplication = CType
    (sender, HttpApplication)
    Dim ctx As
    HttpContext = app.Context

    If Not ctx.Session Is Nothing Then.
         ' Do something cool
    End If

    Does that make sense to do? It stills freaks me out to watch that event fire 2-7 times though. This will not happen outside of VS, say in a production environment would it?

    Many thanks Scott!
    Jeff Spicolie

    Thursday, April 20, 2006 12:27 PM
  • User-1634877164 posted

    Haha -- glad I could help. ;-)

    Yep -- the above code looks like it will work fine to me.

    This wouldn't have on a production IIS environment assuming that static files aren't mapped to IIS (which by default they aren't).  It is only happening with the built-in web-server because it is configured to process all requests to the site.

    Hope this helps,


    Friday, April 21, 2006 11:27 AM
  • User-391155676 posted


    I was working on this with one of our customers and while debugging the code we found out that one of the requests going back to the server was for the webresource.axd file. Now the webresource.axd is a handler which is designed to retrieve assembly resources and serve them to the web browser. These resources could be any static files embedded within the assembly itself. For example, the WebUIValidation.js (used by the validation controls) is now a part of the system.web.dll. So the number of times the OnAcquireRequestState is fired depends on the number of embedded resources we are getting from the server.

    Since this is just a AseemblyResourceLoader there is no session associated with it and we see the null value when we try to get the Session object.

    To workaround this problem we can check the URL and verify if it has an extension of ".axd" and return from there. For example

    if InStr(HttpContext.Current.Request.Url.AbsoluteUri, ".axd") > 0 then
    end if

    Here is an article i wrote a while back on web resources.

    Hope that helps!!!


    Wednesday, May 24, 2006 3:39 PM