Random System.TimeoutException due to timeout occurred after 30000ms

Greetings forum members,

I am getting TimeoutException at random points while trying to perform MongoDB operations such as
(1) list all collections (eg. List collist = database.ListCollectionNames().ToList():wink: , (2) insert a document (eg. lookup.InsertOne(data):wink: etc
and few other points.

Let me give you an insight of what I am doing:

I am using MongoDB.Driver 2.17.1 (integrated and updated using NuGet Manager within Visual Studio 2022). I am using MongoDB as service (Atlas) and cluster used is free/shared. The connection string format was taken from MongoDB (Cloud: MongoDB Cloud…) and using Connect button option, I got

var settings = MongoClientSettings.FromConnectionString("mongodb+srv://<username>:<password>@cluster0.asjuixz.mongodb.net/?retryWrites=true&w=majority");
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
var client = new MongoClient(settings);
var database = client.GetDatabase("test");

and used this in methods where db operations were used. I made changes in user name, pass word, data base name, and collections names.

Now to the exception I am getting, following exception happened:

System.TimeoutException: A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", Type : "Unknown", State : "Disconnected", Servers : [] }.

Stack Trace: 
Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
MongoClient.AreSessionsSupportedAfterServerSelection(CancellationToken cancellationToken)
MongoClient.AreSessionsSupported(CancellationToken cancellationToken)
MongoClient.StartImplicitSession(CancellationToken cancellationToken)
OperationExecutor.StartImplicitSession(CancellationToken cancellationToken)
MongoDatabaseImpl.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
MongoDatabaseImpl.ListCollectionNames(ListCollectionNamesOptions options, CancellationToken cancellationToken)*
MongoData.Method()....

[I’ve omitted some custom code segments here as I’m bound not to reveal those in public forums, but I’ve put here all the relevant MongoDB exception text.]

Following is a snippet of what I’ve done for MongoDB connection and to get collection.

var settings = MongoClientSettings.FromConnectionString(connectionstring);
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
settings.ConnectTimeout = TimeSpan.FromSeconds(10);
settings.RetryReads = true;
settings.RetryWrites = true;
var client = new MongoClient(settings);
var database = client.GetDatabase("odx_db");
List<string> collist = database.ListCollectionNames().ToList();

I’ve tried setting connection timeout, retry reads, retry writes, etc but I am not able to resolve the issue. Could anybody help me here, please?