13

I am getting a 'session state not available in this context' error.

enter image description here

The error is nested in the sender parameter of a number of methods within the Global.asax file:

  • Application_BeginRequest
  • Application_AuthenticateRequest
  • Session_Start
  • Application_Error

The error happens on the very first page load (and all page loads thereafter).

I've added a completely new and empty page, WebForm1.aspx, to the project and made it my start page.

You'd think nothing could go wrong on an empty page. No code of mine is executed, as far as I can see, when loading an empty page. But I'm still getting the session state error.

My project works totally fine. No error pages are shown. There is no incorrect behavior at any time.

But the fact that this session error shows up in the sender parameter of my current project bugs me. This error is not generated in a blank, new project.

What can possibly be the cause of this error in my current project?

How can I trace where the error occurs? I can't set breakpoints in ASP.NET code that's under the hood. Or can I?

Here's my session configuration:

<pages enableSessionState="true" />
<sessionState mode="InProc" cookieless="false" timeout="20" />

Update:

I just found out that a brand new blank project also has this error in the sender object. It's visible in the Application_BeginRequest and Application_AuthenticateRequest methods. But the blank project isn't generating the Application_Error event, like my other project is.

Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
Jay
  • 740
  • 4
  • 8
  • 19

2 Answers2

25

You are requesting for session state too early in the cycle. you need to do it in Application_AcquireRequestState

Emmanuel N
  • 7,350
  • 2
  • 26
  • 36
  • I'm not requesting anything. No code of mine is executed prior to seeing this error in the sender object. Note that the listed methods also include the Application_Error method. An error is definitely generated. And I can't tell where it's coming from. – Jay Nov 09 '12 at 22:51
  • 1
    This should be the answer . This is the reason if we are not getting SessionState in any prior event. – Rameez Ahmed Sayad Sep 20 '14 at 16:50
  • Thank you, this solved my problem in the exact same situation as the OP. +1 – Deverill Sep 22 '14 at 16:47
4

"If a tree falls in a forest and no one is around to hear it, does it make a sound?"

Don't watch the Session property at the point where it is not available yet.

Igor
  • 15,833
  • 1
  • 27
  • 32
  • 1
    I'm not. The Application_BeginRequest events that fire after the Session_Start event has fired, are still showing the error. And then there's the Application_Error event which is fired. Once again... this error happens when loading a blank page and as you can see there is no code in the Application_Start that could cause this. – Jay Nov 09 '12 at 22:53