A timeout occured after 30000ms selecting a server using CompositeServerSelector
Asked Answered
E

14

33

I try to deploy my Mongo database in Mongolabs, everything works fine, and I create a new database. Please see my connectionstring.

    public DbHelper()
    {

        MongoClientSettings settings = new MongoClientSettings()
        {
            Credentials = new MongoCredential[] { MongoCredential.CreateCredential("dbname", "username", "password") },
            Server = new MongoServerAddress("ds011111.mongolab.com", 11111),
            //ConnectTimeout = new TimeSpan(30000)
        };

        Server = new MongoClient(settings).GetServer();

        DataBase = Server.GetDatabase(DatabaseName);

    }

but when I try to connect the database it's shows error like:

enter image description here

Ebon answered 9/7, 2015 at 10:1 Comment(4)
Yeah, I have mistaken in connection string building, so the timeout error occurred. Please see the answer below.Ebon
Possible duplicate of MongoDB C# 2.0 TimeoutExceptionMingmingche
I had this error because the port in my connection string did not match the port in my local mongo db configuration and settings. Once they matched, this error ceased.Tickler
@Tickler greatEbon
E
23

I am replacing the connection string method in like below.

new MongoClient("mongodb://username:[email protected]:11111/db-name")

Now it's solved.

Please see the answer from Paul Lemke.

Ebon answered 17/7, 2015 at 4:4 Comment(2)
Yep, MongoLab doesn't like new MongoSettings(new MongoUri("..."))Succinylsulfathiazole
Why didn't you mark his answer as the solution?Buhrstone
I
33

Add "?connect=replicaSet" to the end of your connection string if connecting to MongoLab.

new MongoClient("mongodb://username:[email protected]:11111/db-name?connect=replicaSet")

This JIRA ticket has some details: https://jira.mongodb.org/browse/CSHARP-1160

Basically the default is to connect to a replica set member. But MongoLab's Single-Node settings are actually a single node replica set and this causes us to not trust it. Appending ?connect=replicaSet to your connection string will force the driver to move into replica set mode and all will work.

Found that info here.

Incurrent answered 19/4, 2017 at 1:8 Comment(0)
E
23

I am replacing the connection string method in like below.

new MongoClient("mongodb://username:[email protected]:11111/db-name")

Now it's solved.

Please see the answer from Paul Lemke.

Ebon answered 17/7, 2015 at 4:4 Comment(2)
Yep, MongoLab doesn't like new MongoSettings(new MongoUri("..."))Succinylsulfathiazole
Why didn't you mark his answer as the solution?Buhrstone
B
19

Make sure your current ip address is white-listed in mongo db server. If you change your internet provider new IP needs to be white-listed.

Boaten answered 22/4, 2019 at 19:37 Comment(3)
@RamyDeeb Its not just for the user who posted it , but also for those who are facing similar issues and viewing relevant answersLallygag
@RamyDeeb the answer provided its also for others who has the same issue and can look for other possible solutions. Please, if you ever have another "solution", just add it, this platform is what it was built for!Cryptology
exactly what happened with my azure service...Basildon
H
7

Make Sure your auth db is set correctly.

I ran into this issue when I mentioned only the DB i wanted to connect to , and my auth db was different (other than admin db ).

The db-name in this line is considered as the auth DB .

new MongoClient("mongodb://username:[email protected]:11111/db-name?connect=replicaSet")

Then you can change the selected DB Later

mDb = mClient.GetDatabase(mongoDBName);
Haveman answered 18/6, 2018 at 11:38 Comment(1)
What is the connect parameter in your connection string? I cannot find info on that in the Mongo docs.Banns
M
5

Same Error Message but not encountered with a MongoLabs deployment.

I just encountered the same error listed in the title with an Asp.Net Core App. My issue was due to an IOC configuration issue.

In my IOC container, my wrapped MongoClient instance was configured with a dependency transient lifestyle.

Per The MongoDb C# Driver:

It is recommended to store a MongoClient instance in a global place, either as a static variable or in an IoC container with a singleton lifetime.

I promoted the lifestyle of my object to a singleton and it resolved the issue.

I am using:

  • .Net Core 2.0
  • Mongo C# Driver version 2.5
  • Castle Windsor for my IOC version 3.3.0

Please reference the C# Driver Client section: http://mongodb.github.io/mongo-csharp-driver/2.5/reference/driver/connecting/#re-use

Mabelmabelle answered 28/3, 2018 at 18:26 Comment(0)
P
3

Make sure the database username is also case sensitive. I ran into this issue because of case sensitivity of the username.

Photooffset answered 22/1, 2016 at 17:30 Comment(0)
B
3

It's related with MongoDB connection error. Probably you don't have permissions or you didn't specify the allowed IPs in MongoDB. Please check for example in MongoDB Compose if you are able to connect with your MongoDB Atlas. If you won't be able to connect, that means that you have wrong MongoDB connection string.

Bloodmobile answered 19/4, 2020 at 18:38 Comment(0)
T
2

In my case, while trying to execute the application with docker compose, I forget the environment variables. The problem was resolved by adding the correct environment variables.

Tannie answered 11/4, 2022 at 21:56 Comment(2)
What environment variables have you added?Biathlon
Those related to database connection and ASP NET environment, as I remember from now. Seeing the project now, I can see two: one of them is from MongoDb database connection; the another one is present in the main Docketfile, in API layer: ENV ASPNETCORE_URLS=http://+:5223Tannie
S
1

I had the same issue. I was able to connect to MongoDB Atlas Using MongoDb Compass, but using the same connection string in a C# project I got the error "A timeout occured after 30000ms selecting a server using CompositeServerSelector... ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (61): Connection refused 192.168.1.254:53".

In my case the problem was caused by my Internet provider router. Switching the connection to my iPhone's 4G hotspot solved the connection issue.

Sera answered 16/9, 2020 at 8:35 Comment(0)
M
1

The port 10255 was blocked by my Internet provider/firewall rules. Opening this port at client side fixed the issue.

Marlowe answered 29/4, 2021 at 12:58 Comment(0)
F
0

I had this issue and turned out the database server was 2 hours behind. Once I fixed its time, the timeout issue was resolved with it.

Flatways answered 23/1, 2020 at 10:57 Comment(0)
F
0

I had the same issue where there is a replicaset with name rs0 and I didnt specify the replicaset

So this caused the CompositeServerSelector timeout.

"ConnectionString": "mongodb://server01:27017,server02:27017,server03:27017"

And this

"ConnectionString": "mongodb://server01:27017,server02:27017,server03:27017?connect=replicaSet"

Passing the actual replicaset name fixed it:

"ConnectionString": "mongodb://server01:27017,server02:27017,server03:27017?replicaSet=rs0"
Fungus answered 18/7, 2023 at 7:48 Comment(0)
C
0

The mongo-connection-check tool helped me by showing that the mongodb atlas port could not be connected to. If the mongodb atlas UI does not offer to whitelist your current IP, it does not reliable mean that your IP is in fact whitelisted. So my fix was to reload the mongodb atlas UI and whitelist my current IP. The link to mongo-connection-check was mentioned in Having trouble connecting to Mongo DB from hosting server

Craven answered 11/11, 2023 at 6:35 Comment(0)
W
0

I have to change the connection string adding +srv at the beginning:

mongodb://..... to mongodb+srv://.....

Whiten answered 25/1, 2024 at 19:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.