Why is health check initializing a new instance of BackgroundWorker each time? #54801
Unanswered
Kampfmoehre
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Looking at the code, the Does |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I have implemented
IHealthCheck
in aBackgroundService
that is registered viaservices.AddHostedService<MyWorker>()
and added to health checks viaservices.AddHealthChecks().AddCheck<MyWorker>()
.The health check is implemented in a way, that is indicating healthy once
ExecuteAsync
was called in the worker and until some event happened. However upon testing I saw that the worker was unhealthy despite running correctly. When debugging I could see, that the health check created a new instance of the worker every time.Since I am using .NET 8
AddHostedService
should register the worker as singleton. Why is the health check not recieving the singleton instance then, am I missing something?I worked around that by changing the registration of the worker to the following:
With this the health checks works as expected, however I could not find any documentation or issues with this problem, so why is the "official" way not working as expected?
Here is the relevant code in case it is helpful. Note, the worker is defined in a NuGet package and then inherited in the actual project.
And the main project
Second note: I am using a custom HealthCheckPublisher that writes the health check to a file since the project does not contain any HTTP server.
Beta Was this translation helpful? Give feedback.
All reactions