HealthChecks.MongoDb is the health check package for MongoDb.
$ dotnet add package AspNetCore.HealthChecks.MongoDbThis health check verifies the ability to communicate with MongoDB. It uses the provided MongoClient to list database names or ping configured database.
By default, the MongoClient instance is resolved from service provider.
void Configure(IHealthChecksBuilder builder)
{
builder.Services
.AddSingleton(sp => new MongoClient("mongodb://localhost:27017"))
.AddHealthChecks()
.AddMongoDb();
}
You can additionally add the following parameters:
clientFactory: A factory method to provide MongoClient instance.databaseNameFactory: A factory method to provide database name.name: The health check name. The default is mongodb.failureStatus: The HealthStatus that should be reported when the health check fails. Default is HealthStatus.Unhealthy.tags: A list of tags that can be used to filter sets of health checks.timeout: A System.TimeSpan representing the timeout of the check.void Configure(IHealthChecksBuilder builder)
{
builder.Services
.AddSingleton(sp => new MongoClient("mongodb://localhost:27017"))
.AddHealthChecks()
.AddMongoDb(databaseNameFactory: sp => "theName");
}
MongoDbHealthCheck was letting the users specify how MongoClient should be created (from raw connection string or from MongoUrl or from MongoClientSettings), at a cost of maintaining an internal, static client instances cache. Now the type does not create client instances nor maintain an internal cache and it's the caller responsibility to provide the instance of (please see for more details). Since MongoDB treating clients as singletons and client instances can be expensive to create, it's recommended to register a singleton factory method for . So the client is created only when needed and once per whole application lifetime.
MongoClientMongoClient