2

I am trying to connect to cosmos db, but getting below timeout error.

System.TimeoutException: '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, EndPoint : 'Unspecified/testgp.documents.azure.com:10255' }', EndPoint: 'Unspecified/testgp.documents.azure.com:10255', State: 'Disconnected', Type: 'Unknown' }] }.'

Code:

string connectionString = 
  @"mongodb://USERNAME:PASSWORD@testgp.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);

var db = mongoClient.GetDatabase(DATABASENAME);

IAsyncCursor<BsonDocument> collectionList1 = db.ListCollections();

RoboMongo error: enter image description here

praveen
  • 41
  • 7

1 Answers1

0

I don't know if it could help in this case but I had similar issues (request timeout) and spent a lot of time finding out when trying to connect to a cosmosdb with mongodb api (from a databricks spark cluster). The problem was the connection string syntax given by azure (exactly the same you show in your example, namely : "mongodb://USERNAME:PASSWORD@testgp.documents.azure.com:10255/?ssl=true&replicaSet=globaldb") that did not include neither the collection nor the database names.

To solve the connection issue, I changed the connection string to: "mongodb://USERNAME:PASSWORD@testgp.documents.azure.com:10255/DATABASE_NAME.COLLECTION_NAME?ssl=true&replicaSet=globaldb"

In some way, I understand that the MongoDB driver wants to deal with a MongoDB instance of the CosmosDB and cannot communicate directly with the CosmosDB itself. Does this make sense?

5md
  • 121
  • 1
  • 5