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