会话在web.config超时之前结束

时间:2016-12-12 16:04:29

标签: asp.net asp.net-mvc-4 session ninject session-state

我有一个ASP.NET MVC应用程序,我在web.config中为其配置了会话,如下所示:

<!--
        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>

仅供参考,我们的生产环境有四个应用程序服务器,我假设为什么设置了DefaultSessionProvider(由其他人设置)。

我使用SessionHelper类在会话中存储信息:

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;
    }
}

我使用Ninject像这样注射HttpContextBase

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

我所看到的是,在我在web.config中设置的65分钟超时之前,会话变量为null。我还注意到我的Session_End()被调用了Global.asax.cs

我已经完成了一些研究,我发现过早结束会话的信息似乎集中在会影响所有用户的事情上,例如bin目录中的文件被更改导致应用程序池重新启动。像这样的东西。我相信我的问题是逐个会议的。

为什么我的会话在我想要之前结束?提前谢谢。

0 个答案:

没有答案