Skip to content

Health actuator cannot handle health checks depending on scoped services #1635

@christianerbsmehl

Description

@christianerbsmehl

Describe the bug

I've recently updated one of our projects to Steeltoe 4.0. When calling the /actuator/health endpoint, I can see that the health check of my EF DBContext is throwing an exception:

   "MyDbContext": {
      "status": "UNKNOWN",
      "details": {
        "exception": "Cannot resolve scoped service 'MyApi.MyDbContext' from root provider."
      }
    },

I found out that since Steeltoe 4.0, all health actuator services are registered as singleton and the IHealthCheck implementations which depend on scoped services cannot be executed. I think the HealthAggregator should create an IServiceScope before executing the health checks to solve this issue.

Since I'm using the .AddDbContextCheck<TContext>() extension method of Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore I have no option to create the IServiceScope by myself. Also, DbContext DI registrations should always be scoped so I cannot change kind of the registration here.

Steps to reproduce

Steps to reproduce the behavior:

  1. Create and register a scoped service builder.Services.AddScoped<MyScopedService>()
  2. Create an implementation if an IHealthCheck that depends on the scoped service
  3. Register the health actuator
  4. Register the health check using builder.Services.Addhealth(builder.Configuration).AddCheck<MyScopedHealthCheck>("ScopedCheck")
  5. Call the /actuator/health endpoint
  6. Observe the 'Cannot resolve scoped service 'MyScopedService' from root provider.' message

Expected behavior

The health check should execute without errors.

Environment (please complete the following information):

  • Steeltoe Version: 4.0.0
  • Platform: Windows DEV machine, Cloud Foundry
  • OS: Win 11, Linux
  • .NET Version 9.0

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context or links

Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component/ManagementIssues related to Steeltoe Management (actuators)ReleaseLine/4.xIdentified as a feature/fix for the 4.x release lineType/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions