Unable to connect to a member of the replica set matching the read preference Primary

In My .NET application i’m getting below error.

MongoDB.Driver.MongoConnectionException: Unable to connect to a member of the replica set matching the read preference Primary
at MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.ThrowConnectionException(ReadPreference readPreference)
at MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.Connect(TimeSpan timeout, ReadPreference readPreference)
at MongoDB.Driver.Internal.MultipleInstanceMongoServerProxy.ChooseServerInstance(ReadPreference readPreference)
at MongoDB.Driver.MongoServer.RequestStart(MongoDatabase initialDatabase, ReadPreference readPreference)
at MongoDB.Driver.MongoDatabase.RequestStart(ReadPreference readPreference)
at MongoDB.Driver.MongoDatabase.RequestStart()

Hi, @Rajendra_Kulkarni,

Welcome to the MongoDB Community Forums. This error message indicates that you are attempting to perform an operation targeted at the primary and the cluster does not currently have a primary. For example if your primary stepped down due to a maintenance event, the cluster can be momentarily without a primary. Another example would be a network partition that prevented the primary from communicating with the other cluster members forcing it to step down. Typically these issues resolve themselves within a few seconds. You can read more about Replica Set Elections in our documentation.

You can investigate this issue by connecting to your cluster using the Mongo Shell (mongosh) and running rs.status() to see the current status of each cluster member.

Hope this helps.

Sincerely,
James

Thanks @James_Kovacs,

I’ve checked the rs.status() and i found all the nodes in cluster are up and running

Hi @James_Kovacs
below is my rs.conf() result

rs.conf()
{
_id: ‘test-5tb57w-shard-0’,
version: 6,
term: 18,
protocolVersion: Long(“1”),
writeConcernMajorityJournalDefault: true,
members: [
{
_id: 0,
host: ‘test-5tb57w-shard-00-00.4srdz.mongodb.net:27017’,
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 6.5,
tags: {
provider: ‘AZURE’,
region: ‘INDIA_WEST’,
workloadType: ‘OPERATIONAL’,
diskState: ‘READY’,
nodeType: ‘ELECTABLE’
},
horizons: {
MESH: ‘testmongodb-shard-00-00.4srdz.mesh.mongodb.net:30522’,
PRIVATE: ‘testmongodb-shard-00-00-pri.4srdz.mongodb.net:27017’,
PUBLIC: ‘testmongodb-shard-00-00.4srdz.mongodb.net:27017
},
slaveDelay: Long(“0”),
votes: 1
},
{
_id: 1,
host: ‘test-5tb57w-shard-00-01.4srdz.mongodb.net:27017’,
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 7,
tags: {
nodeType: ‘ELECTABLE’,
provider: ‘AZURE’,
region: ‘INDIA_WEST’,
workloadType: ‘OPERATIONAL’,
diskState: ‘READY’
},
horizons: {
MESH: ‘testmongodb-shard-00-01.4srdz.mesh.mongodb.net:30522’,
PRIVATE: ‘testmongodb-shard-00-01-pri.4srdz.mongodb.net:27017’,
PUBLIC: ‘testmongodb-shard-00-01.4srdz.mongodb.net:27017
},
slaveDelay: Long(“0”),
votes: 1
},
{
_id: 2,
host: ‘test-5tb57w-shard-00-02.4srdz.mongodb.net:27017’,
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 7,
tags: {
diskState: ‘READY’,
nodeType: ‘ELECTABLE’,
provider: ‘AZURE’,
region: ‘INDIA_WEST’,
workloadType: ‘OPERATIONAL’
},
horizons: {
MESH: ‘testmongodb-shard-00-02.4srdz.mesh.mongodb.net:30522’,
PRIVATE: ‘testmongodb-shard-00-02-pri.4srdz.mongodb.net:27017’,
PUBLIC: ‘testmongodb-shard-00-02.4srdz.mongodb.net:27017
},
slaveDelay: Long(“0”),
votes: 1
}
],
settings: {
chainingAllowed: false,
heartbeatIntervalMillis: 2000,
heartbeatTimeoutSecs: 10,
electionTimeoutMillis: 10000,
catchUpTimeoutMillis: -1,
catchUpTakeoverDelayMillis: 30000,
getLastErrorModes: {},
getLastErrorDefaults: { w: 1, wtimeout: 0 },
replicaSetId: ObjectId(“6513d98929dacb1466072289”)
}
}

I’ve recreated the cluster, but still facing same issue.