Common pitfalls and challenges
Asynchronous programming brings with it a series of pitfalls and challenges. Let’s go through some examples of things that you should be vigilant about when writing asynchronous code in a minimal API:
- Deadlocks: A deadlock occurs when concurrent operations cannot complete due to blocking. In a minimal API, this can be seen when the main thread is blocked. In the following example, the use of
Task.Runcan cause a deadlock because it blocks the main thread:// Deadlock-prone code public async Task<IActionResult> GetData() { var data = Task.Run(() => GetDataFromDatabase()).Result; // Blocking // call return...