Search code examples
c#mongodb.net-4.6.1

MongoConfigurationException when SRV connection string


Driver Language - C#

Driver Version - 2.7.0

DB Version - 4.0.0

.NET Framework 4.6.1

Exception message (credentials and server removed)

The connection string 'mongodb+srv://USER:[email protected]/test?retryWrites=true' is not valid.

Code

var client = new MongoClient(@"mongodb+srv://USER:[email protected]/test?retryWrites=true");

User and password contain no special characters

No connection attempt is made to the server.

However, this works fine if i use the URI version

var client = new MongoClient(@"mongodb://USER:[email protected]:27017,uat-shard-00-01-xxxxx.mongodb.net:27017,uat-shard-00-02-xxxxx.mongodb.net:27017/test?ssl=true&replicaSet=UAT-shard-0&authSource=admin&retryWrites=true");

I've tried to decipher the validation rules from the source but it's beyond my regex ability.

The srv connection string looks fine to me and is exactly the same as that presented to me in the Mongo Atlas UI.

Any idea what i'm doing wrong?

Thanks


Solution

  • I attempted to reproduce this using

    var client = new MongoClient("mongodb+srv://USER:[email protected]/test?retryWrites=true");
    var dbs = await client.ListDatabaseNames().ToListAsync();
    Console.WriteLine(dbs);
    

    Which gave me the expected output (2 database names).

    My test environment is .NET 4.6.1, LINQPad, and C# Driver 2.7.0.

    You can try eliminating the C# Driver (and framework) from the equation by using the Mongo Shell to test. If you're using Mongo Shell version 3.6 or later, you can test the connection with a command similar to

    mongo "mongodb+srv://cluster0-xxxx.mongodb.net/test" --username USER
    

    That being said, you should be able to contact MongoDB Support for your atlas cluster, using the "Support" link on the left side of the Atlas UI. They should be able to help.