4

I developed an ASP.NET MVC 4 web application (.net 4.5) which runs fine in Visual Studio 2012. After deployment to IIS 7 on Windows Server 2008 R2, it seems like the HttpContext.Session object inside my controller is null. I created a simple test ASP.NET MVC 4 application to demonstrate the issue.

In my test app I have a simple Home Controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
          if ( HttpContext != null && HttpContext.Session != null )
          {
              HttpContext.Session[ "test" ] = "a string in session state";
              ViewBag.Info = HttpContext.Session[ "test" ];
              return View();
          }
          else
          {
              if ( HttpContext == null )
              {
                  ViewBag.Info = "beeeeuuuuu - HttpContext = null!!!";
              }
              else if ( HttpContext.Session == null )
              {
                    ViewBag.Info = "beeeeuuuuu - Session = null!!!";
              }
              return View();
          }

    }
}

My Index.chtml view lookes something like this:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
This is a simple test
<p>@ViewBag.Info</p>

So when I run the application I get what I was expecting:

Index
This is a simple test 
a string in session state

But after I deploy the application to the web server the website gives me the following page indicating that the Session object is null:

Index
This is a simple test 
beeeeuuuuu - Session = null!!!

The web application is deployed to the default website which runs under the ASP.NET v4.0 application pool (integrated pipeline).

I already reïnstalled ASP.NET on the server using aspnet_regiis -ir but this didn't help. The Session state is enabled (In Proc) at the ISS server. I hope anyone can help me out here cause I'm trying to solve this for quite some time.

Much thanks in advance and kind regards.

UPDATE: I also tested an ASP.NET MVC 4 build with .NET 4.0 instead of 4.5 and that has the same problem. I also deployed an ASP.NET Web Pages app (.NET 4.0) and that works fine (The HttpContext.Current.Session is not null in the code behind).

UPDATE II: I also tried to store the Session State in a database, which worked fine on my development machine but had the same problem on the production server (HttpContext.Session still returns null).

Gert
  • 401
  • 1
  • 6
  • 10

2 Answers2

28

I found the solution. You have to add and remove the SessionStateModule:

  <configuration>
  ...
  <system.webServer>
    ...
    <modules>
      <remove name="Session" />
      <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
      ...
    </modules>
  </system.webServer>
</configuration>

I have no idea why Microsoft doesn't add this to the web.config of the project template?

Gert
  • 401
  • 1
  • 6
  • 10
0

I thought Session was null in my controller when I deployed my app, but I was wrong. It turned out I had missed the subtleties of a lambda expression. Though this wasn't the solution in the asker's case, this Q&A is highly ranked in searches, so I hope this answer might save someone else some time.

My controller originally contained something like this:

HostingEnvironment.QueueBackgroundWorkItem(ct => Foo(ct, Bar(Session)));

Bar is a static method that gets a value from the session, and it was throwing a NRE trying to access its parameter. I changed my controller code as follows, and all was well:

var bar = Bar(Session);
HostingEnvironment.QueueBackgroundWorkItem(ct => Foo(ct, bar));

The mystery is why the original code ever worked at all when testing on my local machine!

StackOverthrow
  • 1,158
  • 11
  • 23