ConfigureAwait(false) not needed in Console/Win service apps, right?

后端 未结 1 1181
萌比男神i
萌比男神i 2021-02-18 23:54

I have been using async/await for a while, but delved deeper recently, and read a lot of best practice tips saying to by default always use Confi

相关标签:
1条回答
  • 2021-02-19 00:52

    In general, this is true. When working in a Console or Service scenario, there is no SynchronizationContext installed (by default) so the continueOnCapturedContext option in ConfigureAwait will have no effect, which means you can safely remove it without changing the runtime behavior.

    However, there can be exceptions, so I would often suggest writing your code including ConfigureAwait(false) when appropriate anyways.

    The main advantages of including this even in a console or service application are:

    1. The code becomes reusable in other applications later. If you choose to reuse this code, you won't have to track down bugs that arise from not including this.
    2. If you happen to install (or use a library that installs) a SynchronizationContext while running, the behavior of your methods won't change.
    0 讨论(0)
提交回复
热议问题