.NET Session not working - asp.net

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

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!

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.

Related

asp.net webforms modules and handlers section - is remove required?

Below is the default configuration for ASP.NET webforms web.config for specifying handlers and modules under IIS 7 (.5)
When you add custom modules and handlers, I would like to know if remove name ="custom module" is required before adding the module entry. I couldn't find much information on this on whether or why remove is required. I saw some examples where they didn't have removes.
The reason why I'm asking is I vaguely remember reading something about module references will keep adding same entries every time instead of having just one, eventually failing. But I can't find that anywhere now.
If someone can confirm, it will be much appreciated.
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule" />
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory" />
<remove name="ScriptHandlerFactoryAppServices" />
<remove name="ScriptResource" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</handlers>
</system.webServer>
Taking a look in the MSDN, but in the <add> page, you will read this:
The <add> directives are processed in top-down, sequential order. If
two or more <add> subdirectives specify the same verb/path
combination, the final overrides all others.
Based on that, in order for a <add> directive to be overridden, it must have the same verb/path combination. If they do not match, both directives are active and that might cause conflict issues.
Removing the existing directives before declaring your own is a good practice to avoid any conflicts.
Edit: Your application inherits settings from other web.config files as well (See here for more info). Those can be the machine.config, the root web.config etc. Those files can contain module and handler declarations as well. Adding <remove> helps you avoid any conflicts with those files.

Web.config implications of clearing sections?

I've come across code like this:
<modules>
<remove name="ScriptModule"/>
<remove name="YafTaskModule"/>
<remove name="UrlRewriter"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="YafTaskModule" type="YAF.Core.YafTaskModule, YAF.Core" preCondition="managedHandler"/>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule,Intelligencia.UrlRewriter" preCondition="managedHandler"/>
</modules>
And it sometimes breaks, for example if your parent AppDomain doesn't have an UrlRewriter module in it, an error is raised.
How unsafe is it to use:
<modules>
<clear/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="YafTaskModule" type="YAF.Core.YafTaskModule, YAF.Core" preCondition="managedHandler"/>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule,Intelligencia.UrlRewriter" preCondition="managedHandler"/>
</modules>
Same happens in <httpModules> and a few other tags
The main issue with <clear /> is that it will clear all configured elements from all ancestor config files such as parent folder, machine level web.config and machine.config. So in case of <httpModules>, clearing will take out all essential ASP.NET modules such as authentication, session state management, output caching etc. In case your application depends on some of these modules then it will not work.
So the solution is that you must add those modules specifically after clearing the section which is actually correct solution for your problem. Downside is that your config file may become a bit verbose.

ScriptResource.axd and .Net 4

We have just converted our website to run on .NET 4 and upgraded our server (Web Server 2008 SP2 IIS7.0) to run the .NET 4 framework. Our site now displays the following problem on 50% of all computers that try to access it whereas the other 50% work perfectly. All worked fine under .NET 3.5
Sys.WebForms.PageRequestManagerServerErrorException: The requested name is valid, but no data of the requested type was found. ScriptResource.axd Code:0 Line:5 Char:89043
Our web.config has the following defined:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<directoryBrowse enabled="true" />
<modules>
<remove name="ScriptModule"/>
<add name="ASPxHttpHandlerModule" type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v10.1, Version=10.1.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
The site runs with an Application Pool defined as:
.NET Framework Version: v4.0
Managed Pipeline Mode: Integrated
We switched the site to download the Ajax scripts from the MS Content delivery Network (ScriptManager EnableCdn="True") and still had the same problem except this time we get the problem occurring in:
MicrosoftAkaxWebForms.debug.js Line: 868 Char:13
Have removed all our own scripts and the Ajax Control Toolkit to no avail. What is mystifying is that it works ok on 50% of machines and not on the other 50%. There is no commonality between the works/not works. Different o/s different browser mixes. e.g. works fine on one machine Win 7 / IE8 fails on one machine same o/s and browser...works fine on one machine XP Firefox3 fails on another same config.
Any help greatly appreciated (getting desperate!!)
Lastest update:
We reverted the site to .Net 3.5 without changing any code on the site and all works perfectly. Guess we will stick at 3.5 for the foreseeable future!!
Dont give up just yet, had a few headaches when I upgraded our site to 4.0 but nothing a few hours of hair pulling didnt resolve!
Looking at your web.config...
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
These modules are still referencing the old 3.5 libraries. Update your references to point at the new 4.0 versions of these libaries.
Please also look into the changes which have been made in 4.0 to minimise web.config settings. One way to get started would be to create a new 4.0 website work out what you need to add to the web.config to get up and running.
I'm using the AJAX control toolkit on our 4.0 sites and I dont have any of the handlers you have registered and I dont belive you need any of them (apart from maybe
the DevExpress one).
Ta
Steve

ASP.NET Ajax not running with IIS 7

I have a small web application with ASP.NET AJAX running well under the Cassini ASP.NET from http://www.ultidev.com/. But when I tried to host it under IIS7, the AJAX is not working. The UpdatePanel, the Calendar extension, the client side validation from the Validation Control are not working at all. I think it must be a problem of IIS 7.
Here's a section of the web.config file:
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="CrystalImageHandler.aspx_GET" verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode"/></handlers>
</system.webServer>
Can anyone tell me what the problme is with IIS 7? How should I configure it to work with ASP.NET AJAX? Thank you.
Add the following line in the Handlers section in the web.config file:
<add name="Ajax" verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
I just removed Web Server role and added Web Server role again. It's working now. I think maybe some component that IIS 7 was set up in the first place.
Delete the next code line if you added AJAX from NUGET and after you add ajaxcontroltoolkit.dll from
here: http://ajaxcontroltoolkit.codeplex.com/releases/view/109918
<add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit"/>
PD: AJAX CONTROL TOOL KIT 4.5 NOT WORKS WITH FRAMEWORK 4.5. AND AJAX FROM NUGET HAS PROBLEM

Error using session in IIS 7

After deployment of my website to IIS, I'm getting the following error message when trying to access a session:
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.
I access it in Page_Load or PreRender events (I tried both versions). With Visual Studio Dev Server it works without a problem. I tried both InProc an SessionState storage, 1 and multiple worker processes. I added a enableSessionState = "true" to my webpage explicitly.
Here is part of web.config:
<system.web>
<globalization culture="ru-RU" uiCulture="ru-RU" />
<compilation debug="true" defaultLanguage="c#">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<pages enableEventValidation="false" enableSessionState="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>
<httpHandlers>
<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" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="SearchUrlRewriter" type="Synonymizer.SearchUrlRewriter, Synonymizer, Version=1.0.0.0, Culture=neutral" />
<add name="Session" type="System.Web.SessionStateModule" />
</httpModules>
<sessionState cookieless="UseCookies" cookieName="My_SessionId" mode="InProc" stateNetworkTimeout="5" />
<customErrors mode="Off" />
</system.web>
What else do I need to do to make it work?
I tried to monitor if IIS accesses the aspnet_client folder with Process Monitor and didn't get any access.
The solution happened to be very curious. Though IIS7 jn WIndows 2008R2 in error description says to add SessionStateModule to system.web section, it should be added to system.webServer section.
<system.webServer>
<modules>
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</modules>
</system.webServer>
Sessions should work on a plain, empty application. That is, all the default values should work.
If it's not working with basic test applications, then IIS may have got confused with your application.
Try deleting your IIS application. Then create a new virtual directory, restart IIS, and then add your application again.
Also, double check your application's ASP.Net settings. Make sure it's set to the correct version of ASP.Net.
While searching, I also ran into...
http://ramonaeid.spaces.live.com/blog/cns!A77704F1DB999BB0!181.entry
Hope that helps.
Make sure that the service names "ASP.NET State Service" is running and then add this to your web.config.
<sessionState mode="InProc" server="127.0.0.1" port="42424" ... >
Reference:
http://msdn.microsoft.com/en-us/library/ms972429.aspx
http://msdn.microsoft.com/en-us/library/h6bb9cz9(VS.71).aspx
Comment:
Maybe is better to manual change the port to something else. This can be done with regedit.
HKLM\Syste\CurrentControlSet\Services\aspnet_state\Parametres\Port -> 42424
Some more infos:
http://msdn.microsoft.com/en-us/library/ff648667.aspx
Has this particular server been used for anything else? Is it a Windows 2008 server and IIS7?
I'm willing to bet that you're missing some server roles for IIS, have you checked those by right clicking "Computer", choosing "Manage", going to the roles management (it's on the primary tree in computer manager) and checking the installed bits for IIS?
If this is IIS6 then you may have other concerns. But I figure we'll start with something simple, see if we can't help you figure this one out.
What is your folder structure. Do you have web configs at a higher folder that may have explicitly disabled this?
If possible, reregister asp.net
Open Control Panel
Programs\Turn Windows Features on or off
Internet Information Services
World Wide Web Services
Application development Features
ASP.Net <-- check mark here
ref: How to register ASP.NET 2.0 to web server(IIS7)?
Try adding in pages section in web.config the following section:
<namespaces>
<add namespace="System.Web.SessionState" />
<namespaces/>

Resources