10

The .Net console app is in 4.6.1 framework, using MongoDB.Driver 2.8.0. I referred many posts in SO, but I still get the timeout error. Below are the some of the posts I referred

A timeout occured after 30000ms selecting a server using CompositeServerSelector System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector MongoDB C# 2.0 TimeoutException

Below is the code I have used to access the documents from the collection.

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

class Program
{
    static void Main(string[] args)
    {
        string connectionString =
            @"mongodb://mongoaccnt:ADASDXZWADAS2VgsqTYcTS4gtADmB1zQ==@mongocnt.documents.azure.com:10255/?ssl=true&replicaSet=globaldb";

        MongoClientSettings settings = MongoClientSettings.FromUrl(
          new MongoUrl(connectionString)
        );

        settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };

        var mongoClient = new MongoClient(settings);
        string dbName = "app-db";
        string collectionName = "test";
        var database = mongoClient.GetDatabase(dbName);


        var todoTaskCollection = database.GetCollection<test>(collectionName);

        var filter = Builders<test>.Filter.Eq("name", "second");

        var results = todoTaskCollection.Find(filter).ToList();

        Console.WriteLine(results);
        Console.ReadLine();
    }

}

public class test
{
    public string name { get; set; }        
}

Below is the data showing in Azure cloud portal

db.test.find()
Operation consumed 2.31 RUs
{ "_id" : ObjectId("5ca4949fd59b290e00e35eda"), "id" : 1, "name" : "first" }
{
    "_id" : ObjectId("5caafe968f678e0f504c6e64"),
    "id" : 2,
    "name" : "second"
}

Below is the detailed error

System.TimeoutException HResult=0x80131505 Message=A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1,

prvn
  • 406
  • 3
  • 7
  • 24
  • A database command defaults to 30 seconds timeout. It can be made longer. See : http://mongodb.github.io/node-mongodb-native/3.1/reference/connecting/connection-settings/ – jdweng Apr 08 '19 at 12:53
  • @jdweng I added settings.ConnectTimeout = TimeSpan.FromMinutes(5); to the code, the issue persists. – prvn Apr 09 '19 at 06:13
  • 1
    How long does it take before the timeout occurs? If it is still 30 seconds then you are not finding the Server. If it is failing after 5 minutes then it is the query that is taking a long time. The error message I think is saying it can't find the server. So either the server is not running (or not listening on port 10255), or there is no route (ethernet) to the server. I would start by using cmd.exe and trying to ping server >Ping mongocnt.documents.azure.com – jdweng Apr 09 '19 at 09:42
  • I fixed this error by correcting my port in my connection string. It wasnt matching what my local MongoDB was configured to. – AussieJoe Mar 12 '21 at 21:58
  • It's been long since i posted this. It seems the issue was actually MongoDB port was blocked by the organization. Just got to know about it. – prvn Apr 05 '22 at 07:50

2 Answers2

2

Have you tried adding "?connect=replicaSet" after your connection string :

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

Effectively, They've made a distinction between connecting to a standalone server and connecting directly to a replica set member, where the latter is relatively uncommon. Unfortunately, MongoLab's Single-Node settings are actually a single node replica set and this causes us to not trust it. You can fix this by appending ?connect=replicaSet to your connection string. It will force the driver to move into replica set mode and all will work.

you can find more details on : https://groups.google.com/forum/#!topic/mongodb-csharp/O460OHiFjZs

Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
1

It's clear that you didn't add the database name inside the connection code.

Here's the template of the connection string

`var client = new MongoClient("mongodb://<dbuser>:<dbuserpassword>@<mongoaddress>/<dbname>?connect=replicaSet&ssl=true&replicaSet=<replicaset>&authSource=<authsource>");
var database = client.GetDatabase("test");`

You need to fill the following

  • dbuser: user name of the user
  • dbuserpassword: password of the database user
  • dbname: name of the database
  • mongoaddress: address of the mongodb shard
  • replicaset: name of the replica set
  • authsource: Authentication source