, free & fully virtual. Register Now, free & fully virtual. Register Now

C# .NET Core 3.1 - MongoConnectionException - IOException: Unable to read data from the transport connection

I have a .NET Core 3.1 application that uses the MongoDB C# driver (version 2.10.4) and every time I deploy the application that the connection to the DB seems to work for a little bit (maybe 5-10 mins) and then then throws the following exception when trying to read a record from the database:

MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
---> MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server.
---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond..
---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
   at System.Net.Security.SslStream.<FillBufferAsync>g__InternalFillBufferAsync|215_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)\r\n   at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
   at MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync()
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync()
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(Int32 responseTo, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.ConnectionInitializer.InitializeConnectionAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.GetChannelAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.InitializeAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.CreateAsync(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.AggregateOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition`2 pipeline, AggregateOptions options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
   at MongoDB.Driver.AsyncCursorHelper.SingleOrDefaultAsync[T](Task`1 cursorTask, CancellationToken

Based on the exception I thought it was an issue with the Socket so I tried to implement this suggestion:

Unfortunately that didn’t work either, I also tried to set the following values:

ConnectTimeout = new TimeSpan(0,0,0,9000);
MaxConnectionIdleTime = new TimeSpan(0,0,0,9000);
SocketTimeout = new TimeSpan(0,0,0,0, 9000);

But unfortunately nothing seems to work, the connection will keep throwing the same exception posted above over and over. Here is what I currently have:

  • .NET Core 3.1 Microservice
  • Mongo C# Driver 2.10.4
  • Mongo DB 4.0.9 installed on a linux machine
  • Replica Set - the connection is set to connect to a Replica Set, not direct

Any advice or suggestions would be appreciated. Thanks in advance.

HI Friend
I am also same exception after 15-20 min. DId you get any solution for mentioned error?

What worked for me was a combination of making sure to implement a singleton pattern for the connection, in .NET Core it’s something like:

services.AddSingleton<IMongoClient, MongoClient>()

The other important piece ended up being firewall rules. We had several firewall rules and one of them would prevent the connection from reaching the DB server after a while. I would suggested doing a debugging/troubleshooting session with someone that knows the firewall rules and make sure it’s not blocking the connection.

Hope this helps.

Thanks alot. OdotN.
I am working on large data chunk with Mongo DB.
To resolve network and transport related issues with Mongo DB sever. I have done below setting while making connection with MongoDB .

 mongoClientSettings.MaxConnectionLifeTime   = TimeSpan.FromHours(24);
            mongoClientSettings.SocketTimeout           = TimeSpan.FromMinutes(15);
            mongoClientSettings.MaxConnectionIdleTime   = TimeSpan.FromMinutes(20);
            mongoClientSettings.ConnectTimeout          = TimeSpan.FromMinutes(5);
            mongoClientSettings.ServerSelectionTimeout = TimeSpan.FromMinutes(5);
            mongoClientSettings.MinConnectionPoolSize = 10;
            mongoClientSettings.MaxConnectionPoolSize = 100;
  //mongoClientSettings.ClusterConfigurator = cb => cb.ConfigureTcp(tcp => tcp.With(socketConfigurator: (Action<Socket>)SocketConfigurator));

Although i have done/ configured above settings but i still don’t have much knowledge about these parameters.

Are these parameters values ok for big data crunch and don’t impact mongo DB server for long run?