This question is on strike....
You should avoid async void
. async void
is intended for event handlers; most other usages are misuses. async Task
is the natural signature for an async
method without a return value.
So, change ListenForUdpMessages
and ListenForHttpConnections
to be async Task
instead of async void
.
This then brings up the question: "what should I do with those tasks?". Your ExecuteAsync
calls both those methods; it should do something with them. await
ing them one at a time is not a good idea, because you want them both running concurrently. The solution for asynchronous concurrency is to use Task.WhenAll
, something like this:
_logger.LogInformation("Worker running at: {RunningTime}", DateTimeOffset.Now);
var udpTask = _hubService.ListenForUdpMessages();
var httpTask = _managementService.ListenForHttpConnections();
await Task.WhenAll(udpTask, httpTask);
And now your exception handling in ExecuteAsync
will work for exceptions propagated from those methods.
Side notes: