A timeout occured after 30000ms selecting a server while accessing MongoDB in Azure using C#

5

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,

c#
mongodb
azure
timeout
asked on Stack Overflow Apr 8, 2019 by prvn

2 Answers

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
answered on Stack Overflow Jun 7, 2020 by Rami Mohammed
0

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.

answered on Stack Overflow Apr 29, 2019 by Mohit Verma

User contributions licensed under CC BY-SA 3.0