We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Correcting Common Async/Await Mistakes in .NET 8 - Brandon Minnick - NDC Porto 2023
Correct common mistakes in asynchronous programming in .NET 8 with expert tips on thread creation, cancellation, exceptions, and more. Learn best practices to improve performance and reliability in your .NET applications.
-
The
asynckeyword adds about 100 bytes and can lead to unnecessary thread creation. -
The
awaitkeyword can be used to avoid unnecessary thread creation by allowing the thread to be reused. -
Avoid using
async voidas it can lead to exceptions being swallowed and not propagated to the calling thread. -
Use
configure await falseto avoid thread reuse and ensure that the calling thread is reused. - Use a cancellation token to cancel a task that is running in the background.
-
Use
yield returnto allow a method to return partial results instead of waiting for the entire method to complete. -
Avoid using
return awaitas it can lead to unnecessary thread creation. -
Use
try-catchblock to handle exceptions and ensure that the task is cancelled if an exception is thrown. -
Use a
ValueTaskto improve performance by avoiding unnecessary thread creation. -
Use
IAsyncEnumerableto allow a method to return a sequence of values instead of a single value. -
Avoid using
asyncin the constructor as it can lead to issues with the synchronization context. -
Use
configure await falsein the constructor to ensure that the calling thread is reused. -
Use
IProgress<T>to report progress and handle exceptions in a background task. -
Avoid using
asyncin ausingblock as it can lead to issues with the synchronization context.