Connection timeout when writing to Cosmos Db with MongoDb.NET Driver

Greg Quinn 96 Reputation points
2021-09-28T08:36:41.1+00:00

I have an Azure function, I can connect to my CosmosDb in Azure fine, and even get the database.

var client = new MongoClient("mongodb://stor-cosmos-db:[MY_PRIMARY_ACCOUNT_KEY]@stor-cosmos-db.documents.azure.com:443/?ssl=true");  
var database = client.GetDatabase("stor-db2");  

When debugging my code, I get back values from these two rows, so it seems the connection is valid.
But as soon as I try to write something to the database, it just times out...

await database.CreateCollectionAsync("raceresult", new CreateCollectionOptions  
                {  
                    Capped = true,  
                    MaxSize = 1024,  
                    MaxDocuments = 10,  
                });  

I get the value for MY_PRIMARY_ACCOUNT_KEY from the Read/Write keys in the Azure Portal.

135820-image.png

So if these are read/write keys, why do I get a timeout after 30 seconds whenever I try to write something?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,058 questions
{count} votes

Accepted answer
  1. Greg Quinn 96 Reputation points
    2021-09-28T22:35:34.227+00:00

    I resolved this issue by removing the MongoDb .NET connector, and instead installed the Microsoft.Azure.Cosmos NuGet package.
    I could then write to the database without any issues.

    0 comments No comments

0 additional answers

Sort by: Most helpful