.Net控制台应用程序在4.6.1框架中,使用MongoDB.Driver 2.8.0。我在SO中引用了很多帖子,但我仍然得到超时错误。以下是我提到的一些帖子
A timeout occured after 30000ms selecting a server using CompositeServerSelector System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector MongoDB C# 2.0 TimeoutException
下面是我用来访问集合中文档的代码。
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
class Program
{
static void Main(string[] args)
{
string connectionString =
@"mongodb://mongoaccnt:[email protected]:10255/?ssl=true&replicaSet=globaldb";
MongoClientSettings settings = MongoClientSettings.FromUrl(
new MongoUrl(connectionString)
);
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
var mongoClient = new MongoClient(settings);
string dbName = "app-db";
string collectionName = "test";
var database = mongoClient.GetDatabase(dbName);
var todoTaskCollection = database.GetCollection<test>(collectionName);
var filter = Builders<test>.Filter.Eq("name", "second");
var results = todoTaskCollection.Find(filter).ToList();
Console.WriteLine(results);
Console.ReadLine();
}
}
public class test
{
public string name { get; set; }
}
以下是Azure云门户中显示的数据
db.test.find()
Operation consumed 2.31 RUs
{ "_id" : ObjectId("5ca4949fd59b290e00e35eda"), "id" : 1, "name" : "first" }
{
"_id" : ObjectId("5caafe968f678e0f504c6e64"),
"id" : 2,
"name" : "second"
}
以下是详细错误
System.TimeoutException HResult = 0x80131505消息=使用CompositeServerSelector选择服务器30000ms后出现超时{Selectors = MongoDB.Driver.MongoClient + AreSessionsSupportedServerSelector,LatencyLimitingServerSelector {AllowedLatencyRange = 00:00:00.0150000}}。集群状态的客户端视图是{ClusterId:“1”,ConnectionMode:“ReplicaSet”,类型:“ReplicaSet”,状态:“Disconnected”,服务器:[{ServerId:“{ClusterId:1,
您是否尝试在连接字符串后添加“?connect = replicaSet”:
这张JIRA门票有详细信息:https://jira.mongodb.org/browse/CSHARP-1160
实际上,他们在连接到独立服务器和直接连接到副本集成员之间做了区分,后者相对不常见。不幸的是,MongoLab的单节点设置实际上是单节点副本集,这使我们不相信它。您可以通过将?connect = replicaSet附加到连接字符串来解决此问题。它将强制驱动程序进入副本设置模式,一切都会正常工作。
你可以找到更多细节:https://groups.google.com/forum/#!topic/mongodb-csharp/O460OHiFjZs
希望能帮助到你。