I'm trying to understand the behavior of ConfigureAwait(false)
within ASP.NET. Specifically if I don't await
the result. An example of what I mean:
var result = DoSomethingAsync().ConfigureAwait(false);
// do some things other things here. Is our task executing simultaneously?
await result;
From what I've read, in the context of ASP.NET (not core) I understand that:
ConfigureAwait(false)
will prevent the context from being recreated when returning to the application flow. If not specified, the context can still be restored on a different thread to the original.ConfigureAwait(false)
will use the default thread pool scheduler and the AspNetSynchronizationContext
will no longer be involved.AspNetSynchronizationContext
ensures that all tasks will run sequentially (not in parallel) but each task could be run on a different thread.Assuming everything I've said is correct, what happens in the above code sample? From what I've read, that piece of code would cause parallelization. If that's true, would this be considered quite dangerous with ASP.NET, stealing multiple threads per request?
If it's not true, why not? Given AspNetSynchronizationContext
is no longer involved after ConfigureAwait(false)
what is synchronizing the tasks?
UPDATE:
The use of the word 'sequentially' in my third point is incorrect. What I should have said is 'tasks will run one at a time'. That, to me, means not in parallel. This is taken from the information in this article. The table two thirds down describes the feature of each Sync Context. ASP.NET sync context executes one task at a time. https://msdn.microsoft.com/en-us/magazine/gg598924.aspx
Given that ConfigureAwait(false) will continue the execution away from the ASP.NET Synchronization context and onto the default context (which doesn't execute one at a time), could this potentially (but not necessarily) cause a request to use more than one thread simultaneously?
Or am I completely misunderstanding something here?
UPDATE 2:
I've asked a more concise question at:
In ASP.NET Classic can ConfigureAwait(false) cause continuations to execute in parallel on multiple threads?
The ConfigureAwait has no effect on the running of the DoSomethingAsync()
function.
var result = DoSomethingAsync().ConfigureAwait(false);
// do some things other things here. Is our task executing simultaneously?
await result;
and
var result = DoSomethingAsync();
// do some things other things here. Is our task executing simultaneously?
await result.ConfigureAwait(false);
behave exactly the same. ConfigureAwait only modifies the behavior of the await
statement.
Even if you had .ConfigureAwait(true)
you still could be executing tasks simultaneously, the only thing that changes is how the context after the await
is set up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With