User718448626 posted
How to I catch the login authentication exception for MongoDB in aspnet core 3.1 without it quiting the whole app. The code below does trap the error but it also end the app.
Mongodb.Driver v2.11.0
try
{
_db = client.GetDatabase(_options.Value.Database);
// OpenDatabase();
PrepareIndexes();
PrepareReferenceTable();
_dbStatus.Status = 4;
//PrepareTestDemoTable();
}
catch(MongoAuthenticationException authex)
{
_dbStatus.Status = 0;
_dbStatus.Message = authex.Message;
return;
}
catch(MongoException mex)
{
_dbStatus.Status = 0;
_dbStatus.Message = mex.Message;
}
catch(Exception ex )
{
//throw new Exception("Cannot Connect to Database", ex);
_logger.LogDebug("Mongo dB " + ex.Message);
throw ex;
}
MongoDB.Driver.MongoAuthenticationException
HResult=0x80131500
Message=Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
Source=DataApp
StackTrace:
at CompanyApp.DataApp.Database.MongoDB.<PrepareIndexes>d__11.MoveNext() in D:\Project\DataApp\DataApp\Database\DataAppDB.cs:line 130
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in /_/src/System.Private.CoreLib/shared/System/Runtime/ExceptionServices/ExceptionDispatchInfo.cs:line 63
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_1(Object state) in /_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs:line 1954
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() in /_/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs:line 879
at System.Threading.ThreadPoolWorkQueue.Dispatch() in /_/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs:line 677
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() in /_/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs:line 29
This exception was originally thrown at this call stack:
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ProcessReply(MongoDB.Driver.Core.Connections.ConnectionId, MongoDB.Driver.Core.WireProtocol.Messages.ReplyMessage<MongoDB.Bson.RawBsonDocument>)
MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ExecuteAsync(MongoDB.Driver.Core.Connections.IConnection, System.Threading.CancellationToken)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in ExceptionDispatchInfo.cs
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) in TaskAwaiter.cs
MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(MongoDB.Driver.Core.Connections.IConnection, MongoDB.Driver.Core.Connections.ConnectionDescription, System.Threading.CancellationToken)
Inner Exception 1:
MongoCommandException: Command saslStart failed: Authentication failed..