when you have all the configuration stuff done, you should try with adding first the health check definition for MongoDB:
public void ConfigureServices (IServiceCollection services) {
services.AddControllers ();
string mongoDBConnection = Configuration.GetValue ("mongoDB:connection");
services.AddHealthChecks ()
.AddMongoDb (mongodbConnectionString: mongoDBConnection,
name: "todo-db-check",
failureStatus : HealthStatus.Unhealthy,
tags : new string[] { "todo-api", "mongodb" });
}
Also you need to add connection string info for MongoDB in the app settings file.
"mongoDB:connection": "mongodb://localhost:27017"
Then, add the health check with “/hc” path to the request pipeline.
app.UseEndpoints (endpoints => {
endpoints.MapControllers ();
endpoints.MapHealthChecks ("/hc");
});
Now you can run the API and hit the “/hc” endpoint via browser, and it should be in healthy status with its database.
You can also see the documentation for MongoDB (https://docs.mongodb.com/manual/), and and for that specific issue, you should take a look at these pages, to make it clearer.
(https://rmauro.dev/adding-health-checks-to-net-core-application/)
(https://www.gokhan-gokalp.com/en/aspnet-core-series-06-monitor-the-health-of-your-applications-by-implementing-health-checks-and-azure-application-insights/)