0

I'm getting a strange exception in ASP.NET web app relating to the session state. NOTE: The code was produced by a 3rd party....

Basically I'm getting an HttpException with a description of:

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 code where the failure occurs looks like so:

public class ExtendedWebPage : System.Web.UI.Page
{

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (Context.Session != null)
        {
            if (Session.IsNewSession || Session["UserID"] == null)   <--- Errors Here
            {

I'm getting the exception on the test of the if statement, however when I interrogate Context.Session its fine. Up to this point the browser has passed through a Login page which was fine and setup some Session variables which I can see Context.Session, however this.Session is throwing the exeption.

Any ideas?

Thanks,

Philip

4
  • 1
    I think the problem here is that Context.Session and Session are not the same thing. Commented Jan 12, 2011 at 9:50
  • I appreciate Session is a virtual property defined within Page, however Session property uses Context.Session, and returns the exception if the underlying field _session is null. Commented Jan 12, 2011 at 10:04
  • whats the actual error message? Commented Jan 12, 2011 at 10:23
  • Hi Simon831: The error message is "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." Commented Jan 12, 2011 at 10:55

2 Answers 2

1

Have you checked that Session hasn't been disabled for that page in the @Page directive in the aspx markup? Further info here.

Sign up to request clarification or add additional context in comments.

5 Comments

Was abou tto suggest the same thing, this has caught me out before.
Yeah I looked at that, its not explicitly set so I'm assuming its default is to support Session state: <%@ Page Title="" Language="C#" MasterPageFile="Main.Master" AutoEventWireup="true" CodeBehind="CompanyValuations.aspx.cs" Inherits="WoodMac.WebTools.WebCAT.CompanyValuations" %>
Have a look at the web.config and see it the session is globally enabled. If the Session is not explicity set in the Page-directive, it is isherited by the setting in the web.config, which also inherits its settings by the machine.config
The other odd thing having looked at how the Session property is implemented using reflector the exception is only raised when the underlying _session field is null and when _sessionRetrieved is true. _sessionRetrieved is set in the same control path when _session is set to Context.Session, (which I guess can fail). Using the debugger if I set base._sessionRetrieved = false, then call this.Session its fine. Which is very strange indeed is there some sort of race condition perhaps?
I've looked in both the web.config and machine.config for the machine the machine.config has the httpModule and there are no session state directives in the web.config thus should be using default behaviour
0

Finally resolved the issue by renaming the Page and class in the code behind. Creating a new page, copy and pasting the markup into the ASPX and the body of the renamed class into the body of the new class. The code now executes beyond the point where it used to fail.

There's 3 hours I'm not going to get back... :-S

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.