views:

641

answers:

5

I'd like to check for the existence of the Session in a base page class before I use it, but I've found that if it does not exist, it'll throw an exception just by checking:

if (Session != null)
{
    Session.Remove("foo");
}

Will throw this exception:

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the \\ section in the application configuration.

The check happens in the Load event of a base page class that all my aspx pages derive from. The app has the session enabled, and it has the module listed in the httpModules node. This is an app that uses session frequently, normally without a problem.

I get this error only on certain pages and most of the time it's not reliable. I know I should be doing something different to prevent the error, but I'm not sure what?

Am I calling the Session too early in the lifecycle maybe? Am I not checking correctly for whether the session is available?

A: 

You are not experiencing the exception because of a certain Session variable being null, but simply by using Session logic on a page that is not configured to communicate with the session state server (which makes sense in some cases where that is actually a different machine). If it is not an issue for you, consider making sure that you always have enableSessionState set to true.

David Hedlund
A: 

Did you check the pages which are throwing the error for the enableSessionState property of the Page directive? You can do that by going to the .aspx page, and checking whether enableSessionState property is set to true/false. If its set to false, make it true.

Also check in the web.config file for the tag <pages />, and update it to include -

<pages enableSessionState="true" />

Reference: Here

Kirtan
enableSessionState should be a property of the page class? I only see enableViewState as a property.
Pete Michaud
A: 

You can make your check safer - Page.Session wraps a null check around the Context.Session property. So you should be able to retrieve Context.Session and check if it's null. If it is then session state is not available.

But it should be available in Page_Load(), so that points to problems elsewhere :)

blowdart
A: 

Try checking for HttpContext.Current.Session, or even HttpContext.Current (I do that a lot in Unit Testing, when the components are called from the ASP.NET I got those vars, but when I call them from NUNIT I don't have them available)

Andrea
A: 

You can use a method like this to determine if the current request uses session:

    public static bool RequestHasSession
    {
        get
        {
            return (HttpContext.Current.Handler is IRequiresSessionState || HttpContext.Current.Handler is IReadOnlySessionState);
        }
    }

If you are not sure that you are even running in a web context you'd need to check that HttpContext.Current was not null beforehand.

Rob Kent