会话状态提供程序如何在Application_Start中设置

时间:2013-01-23 13:46:08

标签: asp.net session-state session-state-provider

我需要能够在代码而不是web.config中设置会话状态提供程序。我尝试在Application_Start中添加代码

 System.Configuration.Configuration _configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");

 var sessionStateSection =
                (System.Web.Configuration.SessionStateSection)_configuration.GetSection("system.web/sessionState");
            sessionStateSection.Providers.Clear();
            var customSessionStateProvider = new ProviderSettings(DEFAULT_SESSION_PROVIDER, DEFAULT_MEMBERSHIP_PROVIDER);
            customSessionStateProvider.Parameters.Add(CONNECTION_STRING_NAME, DEFAULT_CONNECTION);
            sessionStateSection.Providers.Add(customSessionStateProvider);
            sessionStateSection.Mode = System.Web.SessionState.SessionStateMode.Custom;
            sessionStateSection.Timeout = new TimeSpan(0, 20, 0);
            sessionStateSection.CustomProvider = DEFAULT_SESSION_PROVIDER;

但问题是会话正在恢复为InProc模式,而不是自定义。有没有办法在代码中声明提供者?

1 个答案:

答案 0 :(得分:0)

正如布莱恩韦伯斯特上面提到的,我在此发现的唯一事情就是this page