0

I have an application that will throw exception when there is failure to connect to the database. It seems that this exception causes the Application Pool to be shut down after several failures.

How to catch this exception as not to shut down the application pool? Below is the code that is executed when there is exception

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
    private static readonly SecureLogger log = new SecureLogger(LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType));
    public override void OnException(ExceptionContext filterContext)
    {
        if (!filterContext.HttpContext.IsCustomErrorEnabled)
        {
            return;
        }

        if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
        {
            return;
        }

        log.Error("Internal server error occurred while handling web request.", filterContext.Exception);

        var controllerName = (string)filterContext.RouteData.Values["controller"];
        var actionName = (string)filterContext.RouteData.Values["action"];
        var model = new HandleErrorInfo(new Exception("Internal server error occurred while handling web request."), "Error", "Index");

        filterContext.Result = new ViewResult
        {
            ViewName = View,
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model)
        };

        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
}

The code that causes exception

public List<DataModel> GetStudentData(List<string> idList)
{
    try
    {
        using (MyDBContext dbcontext = new MyDBContext())
        {
            return dbcontext.studentData.Where(x => idList.Contains(x.userId)).Select(
                    x => new DataModel
                    {
                        StudentId = x.userId,
                        StudentName = x.Name
                    }
                )
                ?.ToList();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
rcs
  • 6,713
  • 12
  • 53
  • 75
  • And where exactly is the code that fails located? Does it execute on app start or on request? – Hirasawa Yui Mar 03 '20 at 07:43
  • @HirasawaYui Updated the code portion. It executes on request. But during the execution, there is undergoing database maintenance so it can't connect to DB. – rcs Mar 03 '20 at 07:52
  • Oh, now I see. How about you handle an exception instead of rethrowing it? You can also try tweaking app pool settings like this (2nd answer) https://stackoverflow.com/questions/18016353/why-does-the-application-pool-automatically-stop – Hirasawa Yui Mar 03 '20 at 07:58
  • @HirasawaYui There are a lot of function calls with such structure. So I'd like to know where does it rethrow so I can catch in the global error handling to handle such case. – rcs Mar 03 '20 at 08:01

0 Answers0