gpt4 book ai didi

c# - 无法使用 Azure Cosmos DB 连接到 Mongo DB

转载 作者:IT老高 更新时间:2023-10-28 13:30:20 27 4
gpt4 key购买 nike

我在 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);

失败并出现错误-

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/botframeworkcosmos.documents.azure.com:10255" }", EndPoint: "Unspecified/botframeworkcosmos.documents.azure.com:10255", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我试过这个解决方案- 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"];

我仍然遇到同样的错误。奇怪的是,同样的代码,昨天还在工作。

更新我删除了数据库并创建了一个新数据库。还是同样的问题。

知道可能是什么问题吗?

最佳答案

我有同样的错误信息(A timeout occurred after 30000ms with Unspecified 也提到)。这是因为我公司的防火墙阻止了 Cosmos Mongo 端口上的出站连接,例如TCP 10255。

我通过在我们公司的网络之外临时运行代码进行了测试,并且错误消失了(我确认当我在公司网络之外再次重新运行它时,它仍然失败)。

因此,添加网络防火墙规则以允许到 TCP 端口 10255 的出站连接应该可以解决此问题。

关于c# - 无法使用 Azure Cosmos DB 连接到 Mongo DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53002562/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com