Azure Cosmos DB - 间歇性 MongoConnectionException / IOException / SocketException

Azure Cosmos DB - intermittent MongoConnectionException / IOException / SocketException

我正在使用 Azure Cosmos DB 4.0 和 MongoDB C# 驱动程序 2.10.4。

大多数时候查询工作正常,但我遇到这样的间歇性错误:

MongoDB.Driver.MongoConnectionException: An exception occurred while sending a message to the server. System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.BeginSend(... at System.Net.Sockets.NetworkStream.BeginWrite --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.BeginWrite at System.Net.Security._SslStream.StartWriting at System.Net.Security._SslStream.ProcessWrite at System.Net.Security._SslStream.BeginWrite

发生该错误时,调用需要 10-25 秒才会失败。

我正在使用 new MongoClient(MongoClientSettings.FromConnectionString(cnstr)) 构建 MongoClient,并且我正在使用带有这些参数的连接字符串 ?ssl=true&replicaSet=globaldb&retrywrites=false

我尝试使用 retryWrites=true(根据 Azure 支持建议)但没有帮助。

我尝试了不同的设置,但也没有用(connect=directmaxIdleTimeMS=30000serverSelectionTimeout=5000mssocketTimeout=10000ms)。

是什么导致了这些异常?

修复 set/force TLS 1.2(基于 this Microsoft document):

//return new MongoClient(connectionString);
var settings = MongoClientSettings.FromConnectionString(connectionString);
settings.SslSettings = new SslSettings()
{
    EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12
};
return new MongoClient(settings);

看起来虽然我的连接字符串有 ssl=true,但它不足以在某些服务器上工作(错误是间歇性的)。 forcing TLS 1.2 通常可以修复相同的潜在错误,所以我假设在 Mongo 中可能是同一个问题 - 它确实解决了问题。