Unable to connect to AWS DocumentDB got "A timeout occured after 30000ms" using C# MongoClient

I am running a C# script to perform insert operations to a cluster. Whenever I try to perform a insert operation to the database, I get an error starting with. The cluster is a part of AWS DocumentDB.

A timeout occured after 30000ms selecting a server using CompositeServerSelector

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 : "2", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 2, EndPoint : "Unspecified/xyz:27017" }", EndPoint: "Unspecified/xyz:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.

        public void connection()
        {
            string template = "mongodb://{0}:{1}@{2}/admin?replicaSet=rs0&readPreference={3}&retryWrites=false";
            string username = "unknown";
            string password = "unknown";
            string readPreference = "secondaryPreferred";
            string clusterEndpoint = "unknown";
            string connectionString = String.Format(template, username, password, clusterEndpoint, readPreference);

            string pathToCAFile = @"E:\path\rds-combined-ca-bundle.pem";

            X509Store localTrustStore = new X509Store(StoreName.Root);
            X509Certificate2Collection certificateCollection = new X509Certificate2Collection();
            certificateCollection.Import(pathToCAFile);
            try
            {
                localTrustStore.Open(OpenFlags.ReadWrite);
                localTrustStore.AddRange(certificateCollection);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Root certificate import failed: " + ex.Message);
                throw;
            }
            finally
            {
                localTrustStore.Close();
            }

            var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
            var client = new MongoClient(settings);

            var database = client.GetDatabase("admin");
            var collection = database.GetCollection<BsonDocument>("samplecollection");
            var docToInsert = new BsonDocument { { "pi", 3.14159 } };
            collection.InsertOne(docToInsert);
        }

Hiding the credentials as it is confidential. But please help me know how can I resolve the issue?

Hi, @Sawee_Rawal,

Welcome to the MongoDB Community Forums. A timeout exception such as this is indicative that the driver is unable to connect to the cluster. AWS DocumentDB is not a MongoDB product. I suggest reaching out to AWS DocumentDB support on how to troubleshoot this issue further.

Sincerely,
James

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.