我一直在用 3 个实例的副本集测试官方 MongoDB C# 驱动程序。我创建了一个简单的应用程序,它可以循环访问副本集。
我的问题是:是否可以让 C# 驱动程序在我关闭主服务器时自动重新运行查询,而不像现在那样抛出 EndOfStreamException ?
这是我的 MongoServerSettings 初始化代码:
var settings = new MongoServerSettings()
{
ConnectionMode = ConnectionMode.ReplicaSet,
ReplicaSetName = "mongors",
ReadPreference = new ReadPreference(ReadPreferenceMode.PrimaryPreferred),
SafeMode = SafeMode.True,
DefaultCredentials = new MongoCredentials("user", "password"),
Servers = new[] { new MongoServerAddress("server.net", 27020),
new MongoServerAddress("server.net", 27019),
new MongoServerAddress("server.net", 27018)}
};
这是我查询服务器的代码:
while (true)
{
var server = MongoServer.Create(settings);
var db = server.GetDatabase("db");
var collection = db.GetCollection<TaggedAction>("actions");
var query = Query.EQ("_id", id);
var entity = collection.FindOne(query);
Console.WriteLine(DateTime.Now +" " + entity.ActionName);
Thread.Sleep(2500);
}
如果我关闭主服务器,客户端会抛出以下异常:
System.IO.EndOfStreamException was unhandled
HResult=-2147024858
Message=Attempted to read past the end of the stream.
Source=MongoDB.Bson
StackTrace:
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream, Int32 count) in C:\work\rstam\mongo-csharp-driver\Bson\IO\BsonBuffer.cs: line 314
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream) in C:\work\rstam\mongo-csharp-driver\Bson\IO\BsonBuffer.cs: line 281
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage(BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in C:\work\rstam\mongo-csharp-driver\Driver\Internal\MongoConnection.cs: line 478
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message) in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 296
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 253
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 141
at System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source)
at MongoDB.Driver.MongoCollection.FindOneAs(IMongoQuery query) in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCollection.cs: line 557
at MongoDB.Driver.MongoCollection`1.FindOne(IMongoQuery query) in C:\work\rstam\mongo-csharp-driver\Driver\Core\MongoCollection.cs: line 1734
at ConsoleApplication16.Program.Main(String[] args) in Program.cs: line 53
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
如果我只是吞下这个异常并继续循环,一切正常。因此它可以解决问题并切换到另一台服务器。但是,如果驱动程序可以自动处理这个问题,这样它就不会抛出异常,那就太好了。可能吗?