I used to create an instance of MongoClient from the official driver and register it in the DI as a singleton. But now because of design issues, I have to create another MongoClient instance in the other place to connect to the very same Replica Set.
Are there any potential problems? Do the two instances will use the same connection or create two separate connections?
Project is written in ASP.NET Core 3.1
MongoDB driver's version 2.8
MongoDB server uses 4.2
Currently, if all client options are the same, it will work since the underlying mongo client's data is stored on the cluster level. And as it was mentioned in the comments above, mongoClient constructor tries to retrieve this data from the static storage each time when you create a new client.
However, there is a restriction, your MongoClientSettings should not contain the configured ClusterConfigurator option. Otherwise, it will lead to creating two different clusters with all related stuff for example connection pools.
Also, see Re-use section here: https://mongodb.github.io/mongo-csharp-driver/2.10/reference/driver/connecting/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With