我已经在Azure中使用Mongo API创建了一个Cosmos DB数据库。我已经创建了客户端并进行了如下配置-
_mongoDbConnectionString = configuration["MongoDBConnectionString"];
_databaseName = configuration["MongoDBName"];
_client = new MongoClient(_mongoDbConnectionString);
_database = _client.GetDatabase(_databaseName);
_collectionName = configuration["MongoDBCollectionName"];
然后尝试写数据-
_database.GetCollection<dynamic>(_collectionName).InsertOne(data);
失败并显示错误-
使用以下命令选择服务器30000ms之后发生超时 CompositeServerSelector {选择器= MongoDB.Driver.MongoClient + AreSessionsSupportedServerSelector, LatencyLimitingServerSelector {AllowedLatencyRange = 00:00:00.0150000 }。群集状态的客户端视图为{ClusterId:“ 1”,ConnectionMode :“ ReplicaSet”,类型:“ ReplicaSet”,状态:“ Disconnected”,服务器: [{ServerId:“ {ClusterId:1,EndPoint: “未指定/botframeworkcosmos.documents.azure.com:10255”}“, 端点:“未指定/botframeworkcosmos.documents.azure.com:10255”, 状态:“已断开连接”,类型:“未知”,HeartbeatException: “ MongoDB.Driver.MongoConnectionException:发生异常时 打开与服务器的连接。 -> System.Net.Internals.SocketExceptionFactory + ExtendedSocketException:一个 连接尝试失败,因为连接方未正确 一段时间后响应,或建立的连接失败 因为连接的主机无法响应
我尝试了此解决方案-A timeout occured after 30000ms selecting a server using CompositeServerSelector,但没有用。
我还尝试设置此类SSL策略来配置客户端-
_mongoDbConnectionString = configuration["MongoDBConnectionString"];
_databaseName = configuration["MongoDBName"];
MongoClientSettings settings = MongoClientSettings.FromUrl(
new MongoUrl(_mongoDbConnectionString)
);
settings.SslSettings =
new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
_client = new MongoClient(settings);
_database = _client.GetDatabase(_databaseName);
_collectionName = configuration["MongoDBCollectionName"];
我仍然遇到相同的错误。奇怪的是,相同的代码,昨天都正常工作。
更新 我删除了数据库并创建了一个新数据库。还是同样的问题。
任何想法可能是什么问题?
答案 0 :(得分:0)
我有同样的错误消息(也提到了A timeout occured after 30000ms
和Unspecified
)。这是因为我公司的防火墙阻止了Cosmos Mongo端口上的出站连接,例如TCP10255。
我通过暂时在公司网络外部运行代码进行了测试,该错误消失了(我确认当我再次在公司网络内部重新运行它时,它仍然失败)。
因此,添加网络防火墙规则以允许到TCP端口10255的出站连接应该对其进行修复。
答案 1 :(得分:0)
MongoDB驱动程序版本
private string userName = "FILLME";
private string host = "FILLME";
private string dbName = "Tasks";
private string collectionName = "TasksList";
private IMongoCollection<MyTask> GetTasksCollection()
{
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(host, 10255);
settings.UseSsl = true;
settings.SslSettings = new SslSettings();
settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;
MongoIdentity identity = new MongoInternalIdentity(dbName, userName);
PasswordEvidence evidence = new PasswordEvidence(password);
settings.Credential = new MongoCredential("SCRAM-SHA-1", identity, evidence);
MongoClient client = new MongoClient(settings);
var database = client.GetDatabase(dbName);
var todoTaskCollection = database.GetCollection<MyTask>(collectionName);
return todoTaskCollection;
}
public List<MyTask> GetAllTasks()
{
try
{
var collection = GetTasksCollection();
return collection.Find(new BsonDocument()).ToList();
}
catch (MongoConnectionException ex)
{
return new List<MyTask>();
}
}