Mabrouk Mahdhi
2 min readSep 1, 2023

--

This post provides a solid overview of some best practices for using Async/Await in .NET Core API. However, there are a few areas that could benefit from further clarification or correction:

Generalization of Use-Cases: The post suggests that Async/Await is "best used for I/O-bound operations" and "not recommended for CPU-bound operations," which is mostly true but can be misleading. Async/Await can still be beneficial for CPU-bound operations if those operations can be offloaded to a different thread using Task.Run(), thereby freeing up the main thread.

Consistency in Conclusion: The conclusion introduces the use of Task.WhenAll for running multiple tasks in parallel and mentions using ConfigureAwait(false) for CPU-bound work. Neither of these points were discussed in the main content. It would strengthen the article to include sections explaining these topics.

Error Handling: The post does not mention how to handle exceptions that may occur during asynchronous operations, which is a key aspect of building robust applications. A section covering the use of try/catch blocks around await statements or using the Task.ContinueWith method for fault-tolerance would be beneficial.

CancellationToken Usage: While the post talks about using CancellationToken for task cancellation, it does not mention the importance of propagating the token to all awaited operations where applicable. This is crucial for ensuring tasks can be canceled at any point, not just at specific checkpoints.

ConfigureAwait(false) Explanation: The post explains that ConfigureAwait(false) helps avoid deadlocks but doesn’t elaborate on why this is the case. A more detailed explanation would help readers understand not just the "how" but also the "why."

Code Examples: The code snippets are useful, but they are quite simplified. Including more complex, real-world examples would make the post more practical.

Reusability: The title suggests that this is aimed at ".NET Core API," but most of these practices apply to .NET in general, not just .NET Core or APIs. A more inclusive title might attract a wider audience.

--

--

Mabrouk Mahdhi
Mabrouk Mahdhi

Written by Mabrouk Mahdhi

Founder @ CodeCampsis, Microsoft MVP

No responses yet