Context.Session object is null in Application_AcquireRequestState

asp.net, iis-6, iis-7

Solution

You should use `Application_PreRequestHandlerExecute` instead of `Application_AcquireRequestState`

I just fixed same problem, but for c#, translating code it must be something like that:

Private Sub Application_PreRequestHandlerExecute(sender As Object, e As EventArgs)
If TypeOf Context.Handler Is IRequiresSessionState OrElse TypeOf Context.Handler Is IReadOnlySessionState Then
    ' HttpContext.Current.Session should be available
End If
End Sub

Problem

I upgraded my website from .NET 1.1 to .NET 4.0. In my `Global.asax.vb` file I'm trying to access the `Context.Session` object in the `Application_AcquireRequestState` method and I am getting a null reference exception when I first attempt to access the website. I installed the .NET 4.0 version of the website on a different test server using IIS 7.5 and everything seemed to work OK. But when I install the .NET 4.0 version of the website on the original server that uses IIS 6 I'm getting the exception. Any idea why I am getting this exception? Do the different versions of IIS handle the HTTP Pipeline events differently? Or is there a difference from when the Session object becomes available between the two .NET versions I'm using? Any help would be appreciate, thank you!!

Original source