-
Notifications
You must be signed in to change notification settings - Fork 171
Description
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:
- Create and register a scoped service
builder.Services.AddScoped<MyScopedService>() - Create an implementation if an
IHealthCheckthat depends on the scoped service - Register the health actuator
- Register the health check using
builder.Services.Addhealth(builder.Configuration).AddCheck<MyScopedHealthCheck>("ScopedCheck") - Call the /actuator/health endpoint
- 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.