Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddCheck<MySqlHealthCheck> throws InvalidOperationException for missing MySqlHealthCheckOptions #2141

Closed
bgrainger opened this issue Jan 15, 2024 · 2 comments

Comments

@bgrainger
Copy link
Contributor

What happened:

I added a MySQL health check to a simple ASP.NET 8.0 Web API project. When accessing the health check URL I got the following exception:

InvalidOperationException: Unable to resolve service for type 'HealthChecks.MySql.MySqlHealthCheckOptions' while attempting to activate 'HealthChecks.MySql.MySqlHealthCheck'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, object[] parameters)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance<T>(IServiceProvider provider)
Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService.RunCheckAsync(HealthCheckRegistration registration, CancellationToken cancellationToken)
Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService.RunCheckAsync(HealthCheckRegistration registration, CancellationToken cancellationToken)
Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService.CheckHealthAsync(Func<HealthCheckRegistration, bool> predicate, CancellationToken cancellationToken)
Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckMiddleware.InvokeAsync(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

What you expected to happen:

Expected that URL would return Healthy.

How to reproduce it (as minimally and precisely as possible):

Source code sample:

services.AddMySqlDataSource(builder.Configuration.GetConnectionString("mysql"));

services.AddHealthChecks()
	.AddCheck<MySqlHealthCheck>("MySQL");

// Need to add this line to make it work:
// services.AddSingleton(x => new MySqlHealthCheckOptions(x.GetRequiredService<MySqlDataSource>()));

Anything else we need to know?:

This was probably a regression introduced in #2096; I'll take a look at the code.

Environment:

  • .NET Core version: 8.0
  • Healthchecks version: 8.0
  • Operative system: Windows 10
  • Others:
@bgrainger
Copy link
Contributor Author

It works out-of-the-box when using the API recommended in the documentation:

services.AddHealthChecks()
    .AddMySql();

@bgrainger
Copy link
Contributor Author

This is a side-effect of the new API design suggested in #2113 (comment).

Following that change, DbHealthCheck no longer has a default constructor and can't be added with .AddCheck<DbHealthCheck>(). Instead, one has to use a new helper extension method, .AddDbHealthCheck().

This is probably best reclassified as a documentation bug: the new extension methods should always be used to add a database health check. (This could affect users upgrading from a previous version, but it is included in a package with a major version number change.)

@bgrainger bgrainger closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant