39

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 ?

Sampath
  • 63,341
  • 64
  • 307
  • 441

14 Answers14

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.

Community
  • 1
  • 1
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Could you tell me how to check "ASP.NET Session State Manager Service" is running ? – Sampath Jan 15 '13 at 09:59
  • 1
    Either open the Services panel on your machine (start, run `services.msc`) or execute `net start` on a command line. – jessehouwing Jan 15 '13 at 10:17
  • I have update my Post.Please check whether is that the service you have mentioned (red colored one) ? – Sampath Jan 15 '13 at 11:05
  • it did not work for me, but bellow shared answer worked fine for me – Adeem Feb 13 '14 at 07:42
  • @jessehouwing 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. – RyanfaeScotland Apr 02 '15 at 12:07
  • @jessehouwing 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. – RyanfaeScotland Apr 02 '15 at 15:27
  • Perfect!! Thanks – Amit Kumar Verma Feb 06 '20 at 06:03
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.

Nacht
  • 3,342
  • 4
  • 26
  • 41
  • 1
    + 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." – Joe Johnston Sep 24 '13 at 03:03
  • After doing this I also removed the and sections I had manually added to stop the malformed config error. – RyanfaeScotland Apr 02 '15 at 11:55
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.

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • 1
    Thanks!!! registering the module into the `` section worked for me, while the error I had was telling me to register it under `httpModules` – Jérôme MEVEL Feb 06 '17 at 09:34
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!!!! ;-)

  • 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" – Michelle Sollicito Nov 05 '13 at 13:29
6

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

Samara
  • 387
  • 2
  • 9
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
MAFAIZ
  • 681
  • 6
  • 13
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.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
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!

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Adeem
  • 1,296
  • 1
  • 16
  • 30
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.

Sebris87
  • 452
  • 5
  • 10
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)
     {
        .....
        .....
     }
}
Sedat Kumcu
  • 2,191
  • 1
  • 17
  • 13
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

HydPhani
  • 592
  • 6
  • 13
0

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.

Nerdroid
  • 13,398
  • 5
  • 58
  • 69
Praveen
  • 155
  • 2
  • 5
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.

Nikolai
  • 1
  • 1
0

might be sometimes needing a

[WebMethod(EnableSession = true)]

from a web service

Ash
  • 1,269
  • 3
  • 25
  • 49