DirectConnection cannot be used when ConnectionModeSwitch is set to UseConnectionMode while connect with Azure Cosmos DB API for MongoDB

Viewed 308

I want to connect with Azure Cosmos DB API for MongoDB via ASP.NET app. To start I use connection string provided by Microsoft (when creating an instance by Azure)

var client = new MongoClient("mongodb://[myInstanceName]:
[primaryAccountKey]@[myInstanceName].documents.azure.com:10255/?ssl=true&retrywrites=false&replicaSet=globaldb
&maxIdleTimeMS=120000&appName=@mongocosmocoffedb@");

var database = client.GetDatabase("productdb");

var collection = database.GetCollection<Product>("productcollection");
return Ok(collection);

But when I use this connection string I got error

DirectConnection cannot be used when ConnectionModeSwitch is set to UseConnectionMode.

I found this stackoverflow topic and try use

var client = new MongoClient("mongodb://[myInstanceName]:
[primaryAccountKey]@[myInstanceName].documents.azure.com:10255/?ssl=true");

but in this case i got error

MaxWireVersion is not known.
1 Answers

This is the connection mode port issue. The port number you mentioned 10255 is used for geo-replication and that is causing the issue. Post numbers 10255, 10256 map to the instance that has geo-replication.

Change the port number to 10255. 10250 maps to the default Azure cosmos DB API for Mongo DB. In case of using geo-replication, public end point you can use 10255

Refer the following link: https://docs.microsoft.com/en-us/azure/cosmos-db/sql/sql-sdk-connection-modes

Related