DotNetCore.WindowsService
DotNetCore.WindowsService copied to clipboard
Allow onException timer handler to catch exceptions thrown in async m…
Allow onException timer handler to catch exceptions thrown in async methods
Developing my service I came across problem which took me a while to debug. The problem is that when someone passes async lambda method to Timer onStart
handler - onException
handler will not be triggered. I believe it's because of the type of onStart
handler wchich is Action
. One solution would be to just wrap body of lambda function in additional try catch block:
Timers.Start("Poller", 10000, async () =>
{
try
{
await ThrowAsyncException();
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}, (err) => { _logger.LogError($"Handler: {err.Message}"); });
But it does not feel right with a method which exposes explicit handler for this.
Thanks! I am rly busy with other stuff right now so it's taking a while to react to these. It makes sense and I am all for improving debug experience. I kind of dislike the repetition in code this brings. Would be great if this could have been handled with one approach. I might look into this further to see if I can find a better solution, but if not, I'll be happy to merge and release this.