Session object is null in an ASP.NET MVC 4 web application after deployment in IIS 7 (W 2008 R2)

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

In my test application, 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 looks something like this:

 @{ ViewBag.Title = "Index"; } <h2>Index</h2> This is a simple test <p>@ViewBag.Info</p> 

Therefore, when I run the application, I get what I expect:

 Index This is a simple test a string in session state 

But after deploying 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 reinstalled ASP.NET on the server using aspnet_regiis -ir, but that did not help. Session state is enabled (In Proc) on the ISS server. I hope someone can help me here because I have been trying to solve this for quite a while.

Thanks a lot in advance and are kind.

UPDATE: I also tested building ASP.NET MVC 4 with .NET 4.0 instead of 4.5 and had the same problem. I also deployed an ASP.NET Web Pages application (.NET 4.0) and it works fine (HttpContext.Current.Session does not have null code in the code).

UPDATE II: I also tried to save the session state in a database that worked fine on my development machine but had the same problem on the production server (HttpContext.Session still returns null).

+4
source share
2 answers

I have found a solution. You must add and remove 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 is not adding this to the project template web.config?

+27
source

I thought Session was null in my controller when I deployed my application, but I was wrong. It turned out I missed the intricacies of the lambda expression. Although this was not a solution in Askerโ€™s case, these questions and answers are highly appreciated when searching, so I hope this answer can save someone else some time.

My controller initially contained something like this:

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

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

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

The mystery is why the original code even worked when testing on my local machine!

0
source

Source: https://habr.com/ru/post/1494289/


All Articles