问题
Can any one tell me difference between GetAwaiter()
and ConfigureAwait(false)
.
Both of them are used in Async method to solve the deadlock situation and ConfigureAwait
to complete task without using Synchrnoization context. I'm looking for scenarios where we can use GetAwaiter()
and where we use ConfigureAwait(false)
.
I heard if it is library I'm building then I need to use ConfigureAwait(false)
which generates Configurable Awaitable object of Await task. Can I use ConfigureAwait
in Unittest case project or should use GetAwaiter()
which get await task.
回答1:
Extracted from MSDN Docs
Task.GetAwaiter Gets an awaiter used to await this task. See more details here and here.
Task.ConfigureAwaiter Configures an awaiter used to await this task. See more details here and here
回答2:
Here is some guidance:
Async/Await - Best Practices in Asynchronous Programming
and another similar question:
Preventing a deadlock when calling an async method without using await
You can use Rx to mimic async operations in unit tests. I would advise against having actual async in unit tests; it would slow them down and discourage using those unit tests.
来源:https://stackoverflow.com/questions/33835063/difference-between-getawaiter-and-configureawait