Uncaught OperationCancelledException when shutting down IHostedService
Is there a way to detect that OperationCancelledException was thrown specifically due to application shutdown (e.g. pressing "stop" button in IDE)?
19 Replies
I assume the
Run(Async)
method throws this, so you can try-catch thereUnknown User•4w ago
Message Not Public
Sign In & Join Server To View
I assume the issue is the fact that the process cancels due to shutdown, and the question is how you would gracefully shut down when that happens
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
So how would you check
IsCancellationRequested
if the code throws
Then it's too lateUnknown User•4w ago
Message Not Public
Sign In & Join Server To View
Yes so the code throws
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
So it should gracefully shutdown with a try-catch
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
No point in checking
IsCancellationRequested
when the exception already thrownUnknown User•4w ago
Message Not Public
Sign In & Join Server To View
Cant share the original code since it’s proprietary, but the logic is roughly the same as here, taken from an open source article
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
Yeah it’s not in my app
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
Ig if i were to write pseudo code for it, the snippet i just sent would be it
So yeah it best describes what is implemented in my code
Unknown User•4w ago
Message Not Public
Sign In & Join Server To View
Hmm yeah i’ll try to implement that, thanks!♥️