0

I have an ASP.NET MVC application for which I've configured sessions in the web.config as follows:

<!--
        If you are deploying to a cloud environment that has multiple web server instances,
        you should change session state mode from "InProc" to "Custom". In addition,
        change the connection string named "DefaultConnection" to connect to an instance
        of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
-->
<sessionState customProvider="DefaultSessionProvider" mode="InProc" timeout="65">
  <providers>
    <add name="DefaultSessionProvider" 
         type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
         connectionStringName="DefaultConnection" />
  </providers>
</sessionState>

FYI, our production environment has four app servers which is I assume why the DefaultSessionProvider was set (by someone else).

I'm using a SessionHelper class to store information in the session:

public class SessionHelper : ISessionHelper
{
    private readonly HttpContextBase context;

    public SessionHelper(HttpContextBase context)
    {
        this.context = context;
    }

    public int? GetUserId()
    {
        return getSessionValue<int?>(USER_ID_KEY);
    }

    private T getSessionValue<T>(string key)
    {
        var value = context.Session[key];
        if (value == null)
        {
            return default(T);
        }
        return (T)value;
    }
}

I'm using Ninject to inject HttpContextBase like so:

kernel.Bind<HttpContextBase>().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();

What I'm seeing is that before the timeout of 65 mins which I have set in the web.config, the session variables are null. I also noticed Session_End() in my Global.asax.cs being called.

I've done some research and the information I found regarding sessions ending prematurely seems to be focused on things that would affect all users like files being changed in the bin directory causing the app pool to restart. Things like that. I believe my issue is on a session-by-session basis.

Why is my session ending before I want it to? Thanks in advance.

mellis481
  • 4,332
  • 12
  • 71
  • 118
  • If using multiple servers isn't it best to use StateServer rather than InProc?, this protects against app pool restarts – Henry24 Dec 12 '16 at 22:03
  • @Henry24 StateServer sounded like an appealing option when I looked into it until I read you take 15% performance hit by using it. We're using sticky sessions too FWIW. – mellis481 Dec 13 '16 at 13:37

0 Answers0