Closed Eilon closed 5 years ago
Correct me if I am wrong but the IHostedService is transient. So it won't exist anymore once you have completed the start. You need a Singleton that the IHostedService calls some method on if you want to persist anything passed the start call.
The WebHost holds onto the resolved instances and is responsible for stopping them.
We periodically close 'discussion' issues that have not been updated in a long period of time.
We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate.
From @v-zubritsky on August 3, 2018 13:39
I've encountered an unexpected behavior with
IHostedService
implementation, cancellation tokens passed to it andIApplicationLifetime
interface.I have two hosted services, both of which perform some initialization on start, and if it succeeds, they do their work in background. But if initialization fails, I'd like to stop the application at all, as far as this background work is critical.
I inject
IApplicationLifetime
into services and callStopApplication
on it in case of initialization failure. Application does stop, but cancellation tokens, passed to myIHostedServices
and provided further for graceful shutdown handling, are not cancelled for some reason.I have to link those tokens with
IApplicationLifetime.ApplicationStopping
to handle this issue, but this behavior doesn't seem too obvious.Repo to see this behavior reproduced is available by link.
Microsoft.AspNetCore.App 2.1.1
is useddotnet --info
outputsCopied from original issue: aspnet/Home#3384