Async/Await - Best Practices

less than 1 minute read | By Sree Vani Koneru

Deadlocks is a state where two or more threads are stuck indefinitely waiting for each other. This is technically a freeze and not a performance problem. Deadlock happens when the application has a call where AsyncTask awaits a Result. To fix it, the application should not block by using the .Result property of an asynchronous call. Patterns such as marking the function async, or the aspx page as async and calling with ‘await’ will fix this issue. If a Deadlock occurs due to Task.Result getting called inappropriately, then the whole application is unstable.

This issue is known and nicely documented at the following places.

https://weblogs.asp.net/pglavich/asp-net-web-api-request-response-usage-logging http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#async-all-the-way

Asp.net core: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#avoid-using-taskresult-and-taskwait