8 await async mistakes that you SHOULD avoid in .NET

308,234
0
Published 2020-09-08
Become a Patreon and get source code access: www.patreon.com/nickchapsas
Check out my courses: dometrain.com/

Hello everybody I'm Nick and in this video I wanna talk real world mistakes that you might encounter regarding await async. It's an interesting topic that many people tend to get wrong and this comes down to how Microsoft handled the original rollout of the feature. In this video I will go through 8 real world scenarios of how it can be done wrong and talk about how we can fix them.

Timestamps:
0:00 - Intoduction
1:19 - Once async always async
2:46 - Async void is BAD
6:05 - Prefer Task.FromResult over Task.Run
8:00 - Avoid .Result and .Wait
9:37 - Prefer await over ContinueWith
11:19 - Always pass the CancellationToken
15:16 - Prefer async Task over Task
17:11 - Don't sync over async in constructors

Raw coding video:    • C# Async/Await/Task Explained (Deep D...  
David Fowler's page: github.com/davidfowl/AspNetCoreDiagnosticScenarios…
ValueTask: devblogs.microsoft.com/dotnet/understanding-the-wh…

Don't forget to comment, like and subscribe :)

Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn

#dotnet #await #async

All Comments (21)
  • @RawCoding
    Thank's for the mention :) also a very needed video on youtube, well done. One thing I could reccomend is to increase the font on Rider's menu's
  • @protaties
    "You should avoid async void". Well that's where the word "avoid" from.
  • @MrBarralex
    Dude the cancelation token at endpoint lvl was awesome.
  • @logank.70
    Something I did in a project when needing a resource loaded within a constructor was to build something that let me lazily load the data within an asynchronous context and await when I need it. It was just a small class built on top of Lazy that I called AsyncLazy. I can still pass in my interface that does the asynchronous call but stuff it inside of AsyncLazy and whenever I need that data I can just do (await _settingsLazy).; Plus, in my opinion, I like the way it reads. It tells whoever is reading that piece of code that the data is lazily loaded, cached after the first call, and is done within an asynchronous context.
  • @metlic5209
    About the last case, when you need to resolve service from DI, there is an article at msdn 'Dependency injection guidelines' with anti-pattern examples where is shown how you can deadlock your thread with an async factory.
  • @fahtihi
    Really appreciate the timestamps in the description. Keep up the good work
  • @MINDoSOFT
    Love the information shared ! Thank you Nick. Learned a lot from the "Always pass the CancellationToken" and the "Don't sync over async in constructors" !
  • @Reza-zt4sx
    Hi Nick, thank you for your hard work and knowledge sharing here. It would be great if you elaborate about best practices on error handling. It is one on the wide and most useful topics. There are a lot of videos and resources on the internet, but still some of the ideas behind them are not clear for me. Thanks.
  • @casperes0912
    I'm more curious about the async await mistakes I should be making
  • @victorcomposes
    Ah man, I should update my personal project... Thanks alot Nick.
  • I really liked the way to pass a factory to a constructor that you need to make async. Very enlighting. Thank you Nick!
  • Brilliant gotchas picked. Very concise and understandable demo sequencing. You are a very good teacher, sir.
  • Great content Nick. You should have also shared some insight on 'ConfigureAwait' 😉
  • @BlazarVision
    An excellent video going over Async. Lots of great information here!
  • Thank you for this video, now I know that async/await in our codebase is really messy.
  • @lnagy88
    async Task is dangerous if it's an event handler, basically the Task object will not be assigned and not GC-ed, thus catching exceptions will not work. async void is the way to go when used in event handlers or expect to not GC.
  • @martinprohn2433
    Hi Nick, can you explain to me, why should I write "return await new ValueTask(numberToAdd*2);", if I could also write "return numberToAdd*2;" directly. To explain more, we are in an asyc method, so return a value directly is automatically wrapped in a Task (or in this case a ValueTask). So what is the benefit of this additional await?
  • @Arestkaramazov
    Thank you so much for this video it has been enlightening!!
  • Very nice indeed, especially the cancellation token demo 👍. Keep posting more videos about this topic 👏👏