❔ How to restart a BackgroundService
Searching the internets, I seem to be finding answers that don't really seem right, and not good even if they are right.
Is there a way to get the instance of a hosted service that may have stopped so that I can restart it?
I tried this, but service ended up being
null
in this case:
4 Replies
you'd have to register it manually
if you're registering it currently with
.AddHostedService<T>()
, that only registers it as IHostedService
you would need an alias registration
I wouldn't recommend this, though
that's going to screw up the lifetime management of the serviceYeah makes sense. And actually after thinking about it more, I realized that the service wasn't actually stopped. The
ExecuteAsync
method calls 2 method and one of them was working, and the other wasn't. So I'm guessing maybe the cancellation token cancelled or an exception was thrown and returned out of the methodyeah, what you'd want to do if you experience one of your services crashing, is put error-handling and restart behavior inside that service itself
that's not really the host's job
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.