ASP.NET MVC 2.0 Session State Connection Error Handling

961 Views Asked by At

I have a MVC 2.0 site that uses SQL Server as it's session state manager. I am trying to account for a network error in which my site loses connectivity to the SQL server itself.

For very basic testing purposes I use Application_Error(object sender, EventArgs e) in the Global.asax file to log the error and then I forward the user to a general error page. The problem arises when forwarding to the general error page the Session State Manager attempts to connect again and throws an exception when it can not causing the Application_Error() function to be called again. This results in an endless loop.

Is there a way to disable any attempts to connect to the session state SQL server for a given action in a controller?

1

There are 1 best solutions below

0
On BEST ANSWER

For anyone looking in the future I solved this by using this code in the global.asax file:

protected void Application_Error(object sender, EventArgs e) {
    // Get Exception
    var ex = Server.GetLastError();
    // Looking for SqlTimeout, which is inner exception to HttpExecption
    // when it occurs.
    if (!typeof(HttpException).IsInstanceOfType(ex))
        return;
    // Clear response.
    HttpContext.Current.Response.Clear();
    // Check inner
    if (ex.InnerException != null && typeof(SqlException).IsInstanceOfType(ex.InnerException)) {
        // Clear error
        Server.ClearError();
        // Redirect to basic html error.
        Server.Transfer("/site_down.html", false);
    }
    else {
        // Send to general error page.
        Response.Redirect("~/error/", true);
    }
}