Search code examples
c#mongodbasp.net-coreasp.net-core-webapihealth-monitoring

MongoDb health check and healthCheck UI not working in asp net core


I have simple asp.net core web api that I want to add a health check for mongodb, I have the correct nuget packages and valid connection strings that I verified by connecting to the db manually.

The problem is, I get the following exception when I navigate to http://localhost:56103/health. (I was expecting to see the UI with the health info for mongodb.)

An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'HealthChecks.MongoDb.MongoDbHealthCheck'.

Three questions:

1) What am I missing when setting up the health checks and the service ?

2) Does the .AddMongoDb( . . .) initializes IMongoClient ? is it doing something similar to services.AddSingleton<IMongoClient>(mongoClient); ?

3) What configurations do I have to add to get similar UI (similar what is shown in this doc)

Here is part of Startup.cscode I am working with:

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var config = Configuration.Get<Config>();

            ConfigureMvc(services, config);
            ConfigureDatabase(services, config);
            ConfigureHealthChecks(services, config);


            var containerBuilder = ConfigureDependencyResolver();
            containerBuilder.Populate(services);
            ConfigureLogger(containerBuilder, config);
            _container = containerBuilder.Build();

            return new AutofacServiceProvider(_container);
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //Middleware setups 
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHealthChecks("/health", new HealthCheckOptions
                {
                    Predicate = _ => true,
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                })
                .UseHealthChecksUI();

            app.UseMvc();
        }

        private static void ConfigureHealthChecks(IServiceCollection services, Config config)
        {
            services.AddHealthChecksUI()
                .AddHealthChecks() 
                .AddMongoDb(
                    config.DatabaseSettings.DatabaseConnectionString,
                    config.DatabaseSettings.DatabaseName,
                    null,
                    tags: null)
                .AddCheck<MongoDbHealthCheck>("MongoDb Health Check", null,null);
        }

        private static void ConfigureDatabase(IServiceCollection services, Config config)
        {
            var dbConfig = config.DatabaseSettings;
            var mongoClient = new MongoClient(dbConfig.DatabaseConnectionString);
            var mongoDatabase = mongoClient.GetDatabase(dbConfig.DatabaseName);
            services.AddSingleton<IMongoClient>(mongoClient);
            services.AddTransient(context => mongoDatabase);
        }

Solution

  • So you're trying to use something that should have been inserted via DI. But i'm not sure the AddCheck part is even needed.

    Anyway, to fix the issue, you need the following line in your DI:

    services.AddSingleton(new MongoDbHealthCheck(config.DatabaseSettings.DatabaseConnectionString, config.DatabaseSettings.DatabaseName));