Thought I was getting a handle on ConfigureAwait , then I tried an experiment.
I understand that ConfigureAwait(false) will only matter if there is a synchronization context.
ASP, WPF, etc. should have a context, but console applications and service applications should not.
To find out how this works, I made a web API application and included the following method:
// GET api/values/5 public async Task<string> Get (int id) { var syncCtx = SynchronizationContext.Current; int startThreadId = Thread.CurrentThread.ManagedThreadId; await Task.Delay(TimeSpan.FromSeconds(3)).ConfigureAwait(true); int endThreadId = Thread.CurrentThread.ManagedThreadId; return "Start Thread ID: " + startThreadId.ToString() + ": End Thread ID: " + endThreadId.ToString(); }
My prediction was that without ConfigureAwait or ConfigureAwait set to true , I should see the same thread id before and after the wait.
My first few tests showed that with a true set, as stated above.
Late code runs begin and end on different thread IDs, regardless of ConfigureAwait .
I added syncCtx to convince myself that I have a context.
One caveat I read is that if the task is completed, you will not be guaranteed the same identifier. Is this the case here? If so, why is this so?
Did I install a naive or erroneous test? If so, what would be the right test?
I started this way in a console / service application and realized that I was not getting the same thread id. I added ConfigureAwait(false) , as recommended in most best-practice emails I've seen. Since I like to see how everything works, I tried to check thread ids. Watching them led me to a series of searches that led to the code above.