我正在使用MongoDB驱动程序2.7.3
我一直在寻找有关MongoExceptions的信息
这是我的代码
public IList<Evaluation> GetEvaluations()
{
try
{
return MongoDao.Evaluations.Find<Evaluation>(_ => true).ToList();
}
catch (MongoServerException ex)
{
Console.WriteLine(ex.Message);
throw new InternalErrorDaoException("There is a problem with the mongo server");
}
}
但是,当服务器关闭时,它不会引发异常。
请一些帮助。
答案 0 :(得分:0)
如果错误消息是“意外错误30000毫秒后发生超时”,则您还应该捕获System.TimeoutException
。连接超时时,MongoDB驱动程序将使用它。
public IList<Evaluation> GetEvaluations()
{
try
{
return MongoDao.Evaluations.Find<Evaluation>(_ => true).ToList();
}
catch (TimeoutException ex)
{
Console.WriteLine(ex.Message);
throw new InternalErrorDaoException("Timeout while trying to connect the mongo server");
}
catch (MongoServerException ex)
{
Console.WriteLine(ex.Message);
throw new InternalErrorDaoException("There is a problem with the mongo server");
}
}
如果您想对两个例外使用相同的逻辑:
public IList<Evaluation> GetEvaluations()
{
try
{
return MongoDao.Evaluations.Find<Evaluation>(_ => true).ToList();
}
catch (Exception ex) when (e is TimeoutException || e is MongoServerException)
{
Console.WriteLine(ex.Message);
throw new InternalErrorDaoException("There is a problem with the mongo server");
}
}