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;
}
}