CameraCard
CameraCard

Reputation: 21

Change Session State Provider dynamically

We are currently using cookieless session via the Web.Config

<sessionState cookieless="true" />

When you first hit a web page will do a redirect to itself and mangles the URL to include the session id.

My first question is: For certain page is possible to avoid this? I am trying to change the Session State Provider if the page name = XXXXX. I need it to just show the page without the redirect.

The Second Question is: Is there a way to switch Session providers from Custom to SQL in the Global.asax. In Some cases we need to use SQL others we need Custom.

I have tried Changing the SessionStateMode to SQL in the Global.asax in the Session_Start but no luck. Any ideas?

Edit

As for my first question I have found a Page Directive that will do the trick

EnableSessionState="false"

I still need to figure out how to switch providers in the Global.asax

To Expand We, are currently using cookieless session but we are switching to a Custom Provider(State Server). We need an auto failover to SQL should that server not be available.

Upvotes: 2

Views: 3868

Answers (2)

Sameer Alibhai
Sameer Alibhai

Reputation: 3178

Looks like your answer is here

in global.asax put

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Configuration config;
    config = WebConfigurationManager.OpenWebConfiguration("~");
    SessionStateSection SessionState = config.GetSection("system.web/sessionState") as SessionStateSection;

    if (SessionState != null)
    {
    SessionState.Mode = System.Web.SessionState.SessionStateMode.InProc;
    //SessionState.Mode = (SessionStateSection)"Mode=InProc";
    //(SessionStateSection)"Inproc";
    config.Save();
    }
}

http://www.totaltechnet.com/UpdateConfig/UpdateConfig.htm

Edit: my bad. it looks like this actually writes the new value the web.config

Upvotes: 5

Andomar
Andomar

Reputation: 238076

The global event Session_Start happens after the session is initialized. If you need to make changes before that, you'd have to hook an earlier event, like AcquireRequestState.

Here's a talk from Tech Days Hyderabad about customizing session state using a HTTP module. It suggests SetSessionStateBehavior but that only allows you to enable/disable session state, not to switch provider.

Looks like you could modify your custom provider to read SQL State. Here's a post on how to manually decode session state. That probably won't survive a change in .NET versions though,

Upvotes: 2

Related Questions