6

I have created a Cosmos DB database in Azure and using Mongo API. I have created the client and configured like this-

_mongoDbConnectionString = configuration["MongoDBConnectionString"];
_databaseName = configuration["MongoDBName"];
_client = new MongoClient(_mongoDbConnectionString);
_database = _client.GetDatabase(_databaseName);
_collectionName = configuration["MongoDBCollectionName"];

Then trying to write data-

_database.GetCollection<dynamic>(_collectionName).InsertOne(data);

It fails with error-

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/botframeworkcosmos.documents.azure.com:10255" }", EndPoint: "Unspecified/botframeworkcosmos.documents.azure.com:10255", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I tried this solution- A timeout occured after 30000ms selecting a server using CompositeServerSelector but it did not work.

I also tried setting the SSL policies like this to configure the client-

_mongoDbConnectionString = configuration["MongoDBConnectionString"];
_databaseName = configuration["MongoDBName"];
MongoClientSettings settings = MongoClientSettings.FromUrl(
  new MongoUrl(_mongoDbConnectionString)
);
settings.SslSettings =
  new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
_client = new MongoClient(settings);
_database = _client.GetDatabase(_databaseName);
_collectionName = configuration["MongoDBCollectionName"];

I still get the same error. What is weird is, the same code, it was all working yesterday.

Update I deleted the database and created a new one. Still same problem.

Any idea what could be the issue?

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • You sure your connection string/url is correct in the appsettings.json? The endpoint entry looks a bit odd with that `Unspecified/` in front of `Unspecified/botframeworkcosmos.documents.azure.com:10255` – Tseng Oct 26 '18 at 07:03
  • @Tseng My connection string is like this- `mongodb://chatbotcosmos:@chatbotcosmos.documents.azure.com:10255/?ssl=true&replicaSet=globaldb`. This is my new connection string after I recreated my database which I have copied exactly from Azure. – Souvik Ghosh Oct 26 '18 at 07:34
  • Sure its not getting the connection string from a different source? like appsettings.development.json when you debug it locally? Since the above one says `botframeworkcosmos.documents.azure.com` and yours `chatbotcosmos.documents.azure.com` – Tseng Oct 26 '18 at 07:40
  • @Tseng Yes, I have a new connection string now after I deleted and recreated the database. Also, the config is correct which I have verified in debugging. – Souvik Ghosh Oct 26 '18 at 07:42
  • Do you have some special characters in your password? such as `/`, `:` or `@`? You can't have special chars in there because its an url (or have to escape it maybe) – Tseng Oct 26 '18 at 07:52
  • @Tseng No. The password is auto generated by Azure. Something like this- `12pVpf5Y5spByBjHJkok2D0PfvaQYaTzmDGhZDXO96S0eH6RnQRuPkss4MksNYQjU6mx6d4YSbG3iajuFSkCxQ==`. This was my old password which was working yesterday. – Souvik Ghosh Oct 26 '18 at 07:57

2 Answers2

1

I had the same error message (A timeout occured after 30000ms with Unspecified also mentioned). It was because my company's firewall was blocking outbound connections on the Cosmos Mongo port, e.g. TCP 10255.

I tested this by temporarily running the code outside of our company's network and the error went away (I verified that when I re-ran it again inside out company's network it still failed).

Therefore adding network firewall rule to allow outbound connections to TCP port 10255 should fix it.

user1857450
  • 551
  • 8
  • 20
-1

Check to be Consider:

  1. Connection string from Azure COSMOS DB Environment
  2. MongoDB Driver Version

     private string userName = "FILLME";
            private string host = "FILLME";            
            private string dbName = "Tasks";
            private string collectionName = "TasksList";
    
          private IMongoCollection<MyTask> GetTasksCollection()
          {
            MongoClientSettings settings = new MongoClientSettings();
            settings.Server = new MongoServerAddress(host, 10255);
            settings.UseSsl = true;
            settings.SslSettings = new SslSettings();
            settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;
    
            MongoIdentity identity = new MongoInternalIdentity(dbName, userName);
            PasswordEvidence evidence = new PasswordEvidence(password);
            settings.Credential = new MongoCredential("SCRAM-SHA-1", identity, evidence);
            MongoClient client = new MongoClient(settings);
            var database = client.GetDatabase(dbName);
            var todoTaskCollection = database.GetCollection<MyTask>(collectionName);
            return todoTaskCollection;
        }
    
         public List<MyTask> GetAllTasks()
         {
           try
           {
            var collection = GetTasksCollection();
            return collection.Find(new BsonDocument()).ToList();
            }
            catch (MongoConnectionException ex)
            {
              return new List<MyTask>();
            }
          }
    
Kiran Solkar
  • 1,204
  • 4
  • 16
  • 29