【发布时间】:2019-03-30 20:24:34
【问题描述】:
我在 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);
失败并出现错误-
在选择服务器 30000 毫秒后发生超时 CompositeServerSelector{ 选择器 = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }。集群状态的客户端视图是 { ClusterId : "1", ConnectionMode :“ReplicaSet”,类型:“ReplicaSet”,状态:“断开连接”,服务器: [{ 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"];
我仍然遇到同样的错误。奇怪的是,同样的代码,昨天还在工作。
更新 我删除了数据库并创建了一个新数据库。还是同样的问题。
知道可能是什么问题吗?
【问题讨论】:
-
您确定 appsettings.json 中的连接字符串/url 正确吗?端点入口与
Unspecified/botframeworkcosmos.documents.azure.com:10255前面的Unspecified/有点奇怪 -
@Tseng 我的连接字符串是这样的-
mongodb://chatbotcosmos:<secret_key>@chatbotcosmos.documents.azure.com:10255/?ssl=true&replicaSet=globaldb。这是我重新创建完全从 Azure 复制的数据库后的新连接字符串。 -
确定没有从其他来源获取连接字符串?当您在本地调试时像 appsettings.development.json 一样?既然上面说的是
botframeworkcosmos.documents.azure.com和你的chatbotcosmos.documents.azure.com -
@Tseng 是的,在我删除并重新创建数据库后,我现在有了一个新的连接字符串。另外,配置是正确的,我已经在调试中验证了。
-
您的密码中有一些特殊字符吗?例如
/、:或@?你不能在那里有特殊字符,因为它是一个 url(或者可能必须转义它)
标签: c# mongodb asp.net-core azure-cosmosdb