6
votes

I am NOT using MVC, URL rewriting, custom HTTP modules, etc. On a basic call to the Session property off of either a web page inheriting System.Web.UI.Page or via System.Web.HttpContext.Current.Session, I get the following error:

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 <configuration>\<system.web>\<httpModules> section in the application configuration.

After doing some research, I have implemented Web.config as follows, but the error still occurs. I am debugging the web app out of Visual Studio 2010 using the built-in web server.

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <sessionState mode="InProc" timeout="60" cookieless="false" />
        <pages enableSessionState="true" />
        <httpModules>
            <remove name="Session" />
            <add name="Session" type="System.Web.SessionState.SessionStateModule" />
        </httpModules>
    </system.web>
</configuration>
3
Using your web.config example, I created a new .NET 4.0 web application in Visual Studio 2010 and was able to update a session variable. I'm wondering if there is something else going on in your app that is causing the issue. Could you post a stripped-down version of your app that still has the issue?csm8118
@csm8118 I was too able to update a session variable earlier in the request. However, when I attempt to access that Session variable later in the request that is when I encountered this issue.user786423
at what point in the request are you getting the error? Can you post the code that is throwing the error?csm8118
@csm8118 Here's the flow: 1) User logs in via Default.aspx. If login is successful, Session is set for User in the code-beside for Default.aspx. This assigns just fine. 2) User is redirected to Home.aspx. The code-beside of Home.aspx inherits a custom class called BasePage, which inherits System.Web.UI.Page. In BasePage, it is attempting to access the Session (I have tried via both Session and System.Web.HttpContext.Current.Session), and that is when the exception occurs.user786423
It seems then that Home.aspx is the culprit then. Can you post the .aspx page for Home.aspx? Is there a EnableSessionState="false" in the page directive of Home.aspx (<%@ Page ... at the top of the .aspx)?csm8118

3 Answers

3
votes

Your problem is straight forward. If you shift your code from a place that is executed after Session_Start global event, then you will see this problem go quite comfortably. As an example write your code in Page_Init event or override OnInit and do whatever you want to do with your Session there.

Hope this will work for you.

1
votes

I spent lot of time debugging this issue. Our website is working fine till yesterday and suddenly stopped working with error.

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.

When checked ASP.net State Service is missing from the service list.

reinstalling it as below helped to resolve the issue.

C:\Windows\Microsoft.Net\Framework64\v4.0.30319\aspnet_regiis -i

0
votes

This issue is reproduced in my known scenarios....

If a frameset or a two parallel requests are emerging from browsers, if these both pages have same custombase page, accessing session resulted in this exception due to multiple worker threads...

UPDATE: To all sufferers of this problem, I have my findings here! 1. I could reproduce this issue in IE browser(IE10) and FIREFOX(22), though I have great settings of all enabled..etc.etc... in my local code, WHEN IE settings in Connection Tab --> LAN settings have "Use PROXY Server for Localhost..." checkbox shown as CHECKED - without a VALID PROXY server setting...

When I uncheck this In IE, the same code with DEFAULT Session settings, runs like HORSE!!

Hope that give some light,

Thanks