Asynchronous programming has been around for several years on the .NET platform but has historically been very difficult to do well. Since the introdu

davidfowl / AspNetCoreDiagnosticScenarios Public

submited by
Style Pass
2021-09-24 13:00:10

Asynchronous programming has been around for several years on the .NET platform but has historically been very difficult to do well. Since the introduction of async/await in C# 5 asynchronous programming has become mainstream. Modern frameworks (like ASP.NET Core) are fully asynchronous and it's very hard to avoid the async keyword when writing web services. As a result, there's been lots of confusion on the best practices for async and how to use it properly. This section will try to lay out some guidance with examples of bad and good patterns of how to write asynchronous code.

Once you go async, all of your callers SHOULD be async, since efforts to be async amount to nothing unless the entire callstack is async. In many cases, being partially async can be worse than being entirely synchronous. Therefore it is best to go all in, and make everything async at once.

❌ BAD This example uses the Task.Result and as a result blocks the current thread to wait for the result. This is an example of sync over async.

Leave a Comment
Related Posts