Search code examples
asp.net.netsessionsession-statefault-tolerance

.NET Fault tolerant StateServer


we use a StateServer for handling Session for the known benefits (web farm, IIS recycling).

However I am trying to figure out how to make this fault tolerant. Nothing we store in the Session is critical, it is just used for performance. So if the StateServer is not available we are happy to reload from disk.

However there appears to be no way of detecting if the StateServer is online or not, so the following code all runs fine even if the StateServer is down

try
{
    //It is not NULL as it has been configured
    if (HttpContext.Current.Session != null)
        Session["Test"] = "value";
}
// No exception is thrown
catch (Exception)
{
    throw new Exception();
}

Now it makes sense to me that no exception is thrown. The Session handling would not be very performant if it had to check the status on every write. So I am guessing what happens is that it writes all the Session vaiables when the Response is written.

There lies the problem, when it tries to write the Session it fails with a 500 error and I do not know anyway to intercept this error and handle it.

Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same.

What I would like to happen is that the write just fails silently (or logs an error) and clients are not impacted. As it is write now the entire site goes down due to this single point of failure.

Any ideas - am I missing something obvious?


Solution

  • I would like to accept tgolisch answer as a solution that works for me.

    • In Global.asax we will look for the missing StateServer error in the Application_Error event
    • If we find it we will use Server.ClearError() and log the error
    • We will also use this to log the error and possibly send out an alert

    Thanks all!