How session state provider set in Application_Start

1.2k views Asked by At

I need ability to set the session state provider in code rather than web.config. I have tried adding in code in 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;

But the problem is the Session is reverting back to InProc Mode and not Custom. Is there a way to declare the provider in code?

1

There are 1 answers

0
Deep in the Code On

As Brian Webster mentioned above, the only thing I've found on this is on this page.