No MongoClient despite of providing connection string
Closed this issue · 3 comments
Hi,
I have problem with usage of component on .Net Core with following initialization:
services.AddMigration(new MongoMigrationSettings() {
ConnectionString = ConnectionString,
Database = "my-database",
DatabaseMigrationVersion = new DocumentVersion(0,0,0) //or string e.g. "0.0.0"
});
where ConnectionString = "mongodb://usr:pwd@127.0.0.1:27017/my-database?maxIdleTimeMS=604800000&authSource=admin"
It finishes with:
Mongo.Migration.Exceptions.MongoMigrationNoMongoClientException: 'No MongoClient, you need to register a MongoClient to the DI-Container or define connection settings with MongoMigrationSettings'
.
What can I do wrong?
When I add before
services.AddSingleton<IMongoClient>(mongoClient);
Then no exception is thrown.
same thing! have you found a solution @OldShaterhan ?
@iamdanthedev, due to some changes I don't use it right now, but as far as I remember I did sth like this:
services.AddSingleton(MongoDatabase.Client);
var ConnectionString = "connection-string"
services.AddMigration(new MongoMigrationSettings() {
ConnectionString = ConnectionString,
Database = Environment.GetEnvironmentVariable("MONGO_DATABASE"),
DatabaseMigrationVersion = new DocumentVersion(Environment.GetEnvironmentVariable("MONGO_MIGRATIONS_VERSION")) //(1,0,0)
});
where MongoDatabase
is MongoDB.Driver.IMongoDatabase
, so as there was declared MongoDB client in services it was able to use them. It means that I used already defined client as a base. I hope this will help you.
Hope this will help you resolve it.
Hey @OldShaterhan and @iamdanthedev,
I upadted the Readme, so it is more clear that you need to register a MongoClient befor you configure the Settings.
Cheers,
Sean