I tested the official MongoDB C # driver with a set of replicas of 3 instances. I created a simple application that accesses a set of replicas in a loop.
My question is: is it possible to get the C # driver to restart the request automatically when I close the primary server without throwing an EndOfStreamException, as of now?
Here is my initialization code for 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)} };
And here is the code where I request the server:
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); }
If I disconnect the main server, the client throws the following exception:
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:
If I just swallow this exception and continue the cycle, everything will work. Thus, he can solve the problem and switch to another server. But it would be great if the driver could automatically deal with this, so that in no case would throw an exception. Is it possible?