Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:11:26+00:00 2026-06-12T17:11:26+00:00

I’ve been testing out the official MongoDB C# Driver with a replica set of

  • 0

I’ve been testing out the official MongoDB C# Driver with a replica set of 3 instances. I’ve created a simple app which accesses the replica set in a loop.

My question is: Is it possible to make the C# driver to re-run the query automatically when I close the primary server, without it throwing the EndOfStreamException like it does now?

Here’s my initialization code for the 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’s the code where I query 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 shut down the primary 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 with the loop, everything works. So it can sort out the issue and switch to an another server. But it would be great if the driver could automatically handle this so that in no point it throws the exception. Is it possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:11:27+00:00Added an answer on June 12, 2026 at 5:11 pm

    In many cases, it would be impossible to fail over because the cursor that is executing only exists on the primary server. The secondaries don’t know anything about it and therefore could not continue it.

    In your case, you happen you know you want to continue, but it would presumptuous of us to take your needs and apply them to all situations. Where you want to just continue the loop, others may not.

    Other than that point, some drivers do retry queries. The .NET driver does not because we feel we cannot always ascertain the correct behavior and therefore leave it up to the application to decide.

    In the case of PrimaryPreferred, there is a reason that you want reads to come from the Primary — because they are up-to-date. If we silently fall back to a secondary then, depending on how far back the secondary is, there is a chance that your query actually returns results from before the last successful query to the Primary. This is not a good experience and therefore we simply don’t do it and recommend that you catch these errors and handle the retries yourself.

    We are looking to get some of these errors wrapped in MongoDB specific exceptions so you don’t have to guess at things like an EndOfStream exception (https://jira.mongodb.org/browse/CSHARP-474). In addition, if you would like to see this feature, please file a jira and we’ll look into how we can do this in a predictable manner — perhaps using a user supplied strategy for handling retries (IRetryStrategy or something).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.