我需要能够在代码而不是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模式,而不是自定义。有没有办法在代码中声明提供者?