.NET Session not working

This is a weird one, but hopefully someone can give me an idea here. I’m putting a few values into session in the Session_Start of the Global.asax in my app. Immediately after the Session_Start my base page’s OnInit gets called and tries to use one of those Session variables.

The weird part is sometimes it works, and then after NO changes will start giving me this 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.”

I’ve looked and looked finding all different ways on enabling session in the web.config. Here is what it looks like now:

<system.web>
...
    <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"/>

    <globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
    <httpHandlers>
        <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpHandlers>
    <xhtmlConformance mode="Legacy"/>
    <pages>
    <!-- enableSessionState="true" autoEventWireup="true" enableViewState="true" enableViewStateMac="true" -->
        <controls>
            <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </controls>
    </pages>
    <httpModules>
        <!--<add name="Session" type="System.Web.SessionState.SessionStateModule"/>-->
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
</system.web>

You can see I have some stuff commented out, specifically the enableSessionState="true" and <add name="Session" type="System.Web.SessionState.SessionStateModule"/> because they were never there before and it always used to work. But I’ve tried it with them both enabled, one or the other enabled, it just doesn’t make a difference.

This is an app that has been in production for years, I’m only making minor changes, probably the biggest of which is adding a “titlebar” page to it, and upgrading from 2.0 to 3.5. I haven’t tried adding the enableSessionState to the page directives because it didn’t work in the web.config I don’t see why it would work directly on the page, and I’d hate to dirty the app up anymore or waste anymore time trying in vain to enable session.

Anyone have any other ideas?

Thanks,
Jeff

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

BREAKPOINTS!

I had breakpoints set in the constructor methods of the base page, and the main page, as well as the Session_Start and the OnInit. A coworker deleted all breakpoints in the debugger and the problem dissappeared!

Ugh!

Method 2

I experience the same thing with a breakpoint at a variable instantiation and page load setting some session values. Running Windows 7 / VS2010. When I disable the breakpoint it is then able to load the session state.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x