when should I use ConfigureAwait(true)

后端 未结 5 2019
灰色年华
灰色年华 2020-12-29 01:55

Has anyone come across a scenario for using ConfigureAwait(true)? Since true is the default option I cannot see when would you ever use it.

5条回答
  •  别那么骄傲
    2020-12-29 02:11

    One possibility I can see is if you're writing code in a library and you want to allow your callers to decide whether it's appropriate to continue on the original context1 (although I'd usually argue for never continuing on the original context from within library code)

    Your caller will either pass a bool parameter or set some configuration value, and so you will not know until runtime what the correct argument value is.


    This is the general type of answer for APIs such as this that have a no-args variant and a variant with a single argument, where the no-args variant is documented as "the same as the single argument variant with well-known value x" - if you won't know until runtime what the correct value to pass is, then you should just call the single argument variant with the correct runtime value.


    1 E.g. your caller is also supplying a delegate. Your caller will know (and can decide) whether that delegate needs to be back on the original context or not.

提交回复
热议问题