The top level loop in a hosted service (worker) needs to execute while the
CancellationToken has not recieved a cancellation request. This needs to be wrapped in a generic try/catch as when a BackgroundService throws an unhandled exception, the exception is lost and the service appears unresponsive. This is because the base class
BackgroundService is awaiting the task to complete and return.
As the code in this example was .NET 5 and I needed to test the try/catch is not removed.
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
This code posed two problems
- How does the test break out the
- How do I test a
The second issue can be resolved by additional setup to expose
protected behaviour the first needed the token to be cancelled.
There were two ways this could be done:
- cancelling after a specific amount of time
- mocking a dependnacy and using
Callbackto call the
.Cancel()method on the token
This should work but the test could be flaky (fails to produce the same result each time)
This will only be possible if you have some mocked dependency.