Async Tip: why you should avoid void
The guideline to prefer async method which return Task over async method that return void is quite known,
but why should you follow the guideline?
The first obvious reason to use the guideline is testability.
When you return void the test don’t really know when it is right time to
check for the exception.
The other reason (which is less known) is that running async method which return void and
throw unhandled exception, will crash the process.
If the method would return Task the unhandled exception would be swallow (still available
Check it using the following code:
When you want to expose on your API, but do async operation within the method.
For example assume that you design a logger API, the caller shouldn’t await the log report
while the method will actually do IO call (either writing to the database or sending the log to Application Insight or ELK).
In order to have implementation which return void which doesn’t crash when unhandled exception thrown you can use the following pattern: