Session state can only be used when enableSessionState is set to true either in a configuration
Asked Answered
M

14

41

I am working on Asp.net MVC 2 app with c# by using vs 2010.I am having below mentioned error when I run my app locally under debug mode.

Error message image is as below :

enter image description here

Error message text is as below :

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.

What I did for sort out this issue are as below :

Try 1 : web.config file has been changed like below:

<system.webServer>
       <modules runAllManagedModulesForAllRequests="true">
        <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

Try 2 is as below :

enter image description here

But Unfortunately still I am having same issue which I have mentioned Above.

UPDATE

enter image description here

Do you have any solution for this ?

Mogilev answered 15/1, 2013 at 8:59 Comment(0)
M
45

Did you enable the session state in the section as well?

 <system.web>
      <pages enableSessionState="true" /> 
 </system.web>

Or did you add this to the page?

 <%@Page enableSessionState="true"> 

And did you verify that the ASP.NET Session State Manager Service service is running? In your screenshot it isn't. It's set to start-up mode Manual which requires you to start it every time you want to make use of it. To start it, highlight the service and click the green play button on the toolbar. To start it automatically, edit the properties and adjust the Start-up type.

Or set the SessionState's mode property to InProc, so that the state service is not required.

 <system.web>
      <sessionState mode="InProc" />
 </system.web>

Check MSDN for all the options available to you with regards to storing data in the ASP.NET session state.


Note: It's better to have your Controller fetch the value from the session and have it add the value to the Model for your page/control (or to the ViewBag), that way the View doesn't depend on the HttpSession and you can choose a different source for this item later with minimal code changes. Or even better, not use the session state at all. It can kill you performance when you're using a lot of async javascript calls.

Marissamarist answered 15/1, 2013 at 9:47 Comment(7)
Could you tell me how to check "ASP.NET Session State Manager Service" is running ?Mogilev
Either open the Services panel on your machine (start, run services.msc) or execute net start on a command line.Marissamarist
I have update my Post.Please check whether is that the service you have mentioned (red colored one) ?Mogilev
it did not work for me, but bellow shared answer worked fine for meForeman
@Marissamarist Unless I am mistaken both your pages and sessionState tags need to be closed by ending with a /. My setup is complaining that they aren't.Mahican
@Marissamarist some users can be funny about editing without informing first also SO requires me to change 6 characters whilst you have no minimum limit to edit (I think). However I shall keep your preference in mind and try to honour it should we cross paths again.Mahican
Perfect!! ThanksPremeditation
K
14

also if you are running SharePoint and encounter this error, don't forget to run

Enable-SPSessionStateService -DefaultProvision

or you will continue to receive the above error message.

Kettledrum answered 23/1, 2013 at 23:52 Comment(2)
+ 1 for fixing the primary error, however, when run in IIS8, SQL2012, Win2012 and SP2013 this returns a new error: "Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode."Amazon
After doing this I also removed the <sessionState mode="InProc"> and <httpModules> <add name="Session" type="System.Web.SessionStateModule" /> </httpModules> sections I had manually added to stop the malformed config error.Mahican
M
8

Actually jessehouwing gave the solution for normal scenario.

But in my case I have enabled 2 types of session in my web.config file

It's like below.

First one :

<modules runAllManagedModulesForAllRequests="true">
        <remove name="Session" />
        <add name="Session" type="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>

Second one :

<sessionState mode="Custom" customProvider="DefaultSessionProvider">
            <providers>
                <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="PawLoyalty" applicationName="PawLoyalty"/>
            </providers>
 </sessionState>

So in my case I have to comment Second one.B'cos that thing for the production.When I commented out second one my problem vanished.

Mogilev answered 15/1, 2013 at 14:23 Comment(1)
Thanks!!! registering the module into the <modules> section worked for me, while the error I had was telling me to register it under httpModulesGoatfish
C
7

I want to let everyone know that sometimes this error just is a result of some weird memory error. Restart your pc and go back into visual studio and it will be gone!! Bizarre! Try that before you start playing around with your web config file etc like I did!!!! ;-)

Caerleon answered 4/11, 2013 at 18:34 Comment(1)
I could tell mine was a memory error because it worked fine if I ran my app using "Debug - start without debugging" but it didnt work if I ran "Debug - start debugging"Caerleon
E
6

In my case problem went away simply by using HttpContext.Current.Session instead of Session

Educate answered 2/5, 2014 at 21:40 Comment(0)
L
3

For SharePoint Can find the web config file in C:\inetpub\wwwroot\wss\VirtualDirectories\Sitecollection port number - and Make changes

 <system.web>
  <pages enableSessionState="true" /> 
 </system.web>

and using SharePoint Management Shell Run below Command

   Enable-SPSessionStateService -DefaultProvision
Lanham answered 28/8, 2015 at 6:31 Comment(0)
T
2

I realise there is an accepted answer, but this may help people. I found I had a similar problem with a ASP.NET site using NET 3.5 framework when running it in Visual Studio 2012 using IIS Express 8. I'd tried all of the above solutions and none worked - in the end running the solution in the in-built VS 2012 webserver worked. Not sure why, but I suspect it was a link between 3.5 framework and IIS 8.

Tobias answered 27/6, 2013 at 9:28 Comment(0)
F
1

Following answer from below given path worked fine.

I found a solution that worked perfectly! Add the following to web.config:

<system.webServer>
<modules>
<!-- UrlRewriter code here -->
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />
</modules>
</system.webServer>

Hope this helps someone else!

Foreman answered 13/2, 2014 at 7:42 Comment(0)
A
1

This error was raised for me because of an unhandled exception thrown in the Public Sub New() (Visual Basic) constructor function of the Web Page in the code behind.

If you implement the constructor function wrap the code in a Try/Catch statement and see if it solves the problem.

Anarchic answered 26/11, 2014 at 12:15 Comment(0)
B
1

I have got this error only when debugging the ASP .Net Application.

I also had Session["mysession"] kind of variables added into my Watch of Visual Studio.

the issue was solved Once, I have removed the Session Variables from watch.

Balmoral answered 9/10, 2015 at 2:26 Comment(0)
O
1

If you are using a "Generic Handler(ashx)"; you should add an "inheritance" like below. IRequiresSessionState

public class GhStreamViewsGuncelle : IHttpHandler, IRequiresSessionState
{
     public void ProcessRequest(HttpContext context)
     {
        .....
        .....
     }
}
Orvilleorwell answered 9/3, 2022 at 13:9 Comment(0)
S
0
  1. Could be your skype intercepting your requests at 80 port, in Skype options uncheck
  2. Or Your IE has connection checked for Proxy when there is no proxy
  3. Or your fiddler could intercept and act as proxy, uncheck it!

Solves the above problem, It solved mine!

HydTechie

Suzysuzzy answered 26/8, 2013 at 16:35 Comment(0)
J
0

Session State may be broken if you have the following in Web.Config:

<httpModules>
  <clear/>
</httpModules>

If this is the case, you may want to comment out such section, and you won't need any other changes to fix this issue.

Jelly answered 12/11, 2015 at 10:44 Comment(0)
P
0

might be sometimes needing a

[WebMethod(EnableSession = true)]

from a web service

Pappy answered 13/4, 2021 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.