whoah
whoah

Reputation: 4443

MVC Session loses value

On my localhost server Session["Culture"] variable always store the proper value.
But on my external server, the same session variable (Session["Culture"]) sometimes lose value and become null. Why it is happen? And how to resolve it?

Part of a global.asax.cs file:

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session != null)
        {

            CultureInfo ci = (CultureInfo)this.Session["Culture"];
            if(ci == null){
                //breakpoint
            }
        }
    }

EDIT:

It is obvious that session cannot be shared between server A and server B.
My problem is rather different - in a nutshell- the same application, but:
1) on server A (localhost) Session["Culture"] works properly and it always store some information.
2) on server B (external) Session["Culture"] work nice but always after random time lose value and become null

Upvotes: 0

Views: 1688

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40160

In-process session state is subject to being cleared any time, as sessions are ended when the appdomain is recycled.

You can mitigate this some by using an external session storage mechanism, such as the state service, or a database provider. You can also build your own session state provider.

However; you should still treat session state as only somewhat less volatile than cache, if you want it to be robust; always check for null, and reset the values if so.

If sessions are expiring that quickly on one server, I would look into why that might be. Something may be causing recycles improperly.

Addition: I see you are using multiple servers; in-process session state also cannot handle this. The above advice can help there, too.

Upvotes: 3

Related Questions