To run or not run ConfigureAwait(false) with both services and activities with Xamarin Android ver 8, .net Standard

后端 未结 2 1340
Happy的楠姐
Happy的楠姐 2020-12-21 03:17

I came across articles below regarding when and where to use ConfigureAwait(false), but cannot get an answer.

You Don’t Need ConfigureAw

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-21 04:11

    Is ConfigureAwait(false) required

    ConfigureAwait(false) is never required, unless you're using it as part of a direct blocking sync-over-async hack, which is never recommended.

    If you need to stay on the same context (e.g., you're accessing UI elements), then the question is moot: you cannot use ConfigureAwait(false) because your code must resume on the same context. This is the scenario for your "opposite answer" link.

    For libraries, the traditional approach is to recommend ConfigureAwait(false) everywhere, because library authors don't know how their libraries will be consumed. This was especially true because there were a few situations (primarily in ASP.NET) where sync-over-async hacks were required.

    However, now that ASP.NET Core is async all the way (no longer requiring blocking hacks) and also doesn't have a context at all, the chances that a library will be used with sync-over-async is significantly reduced. So some libraries have started dropping ConfigureAwait(false) - most notably Entity Framework Core. Time will tell whether ConfigureAwait(false) will continue, or whether it will become a historical oddity.

    For myself, I do use ConfigureAwait(false) in my libraries, which are commonly used on older platforms. But if you have a library that is only consumed by modern UI and Core apps, then it's not necessary.

提交回复
热议问题