However, with ASP.NET Web Api, if your request is coming in on one thread, and you await some function and call
ConfigureAwait(false)
that could potentially put you on a different thread when you are returning the final result of your ApiController function.Actually, just doing an
await
can do that. Once yourasync
method hits anawait
, the method is blocked but the thread returns to the thread pool. When the method is ready to continue, any thread is snatched from the thread pool and used to resume the method.
I've just tested that in a console program:
async Task foo()
{
int y = 0;
while (y<5) y++;
}
async Task testAsync()
{
int i = 0;
while (i < 100)
{
Console.WriteLine("Async 1 before: " + i++);
}
await foo();
while (i < 105)
{
i++;
Console.WriteLine("Async 1 after: " + i);
}
}
Calling await foo()
doesn't cause the thread testAsync
was running on to return to thread pool, testAsync
just runs line by line on the same thread from start to end. What's am I missing here?