So I have a continuation defined:
var task = _httpClient.SendAsync(request, cts.Token);
task.ContinueWith(i => { /* TODO: log */ },
TaskContinuationOptions.OnlyOnCanceled);
var response = await task.ConfigureAwait(false);
I get a compiler warning on the ContinueWith
line:
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
However, as you can see, I apply the await
to the response.
When the method that contains this code exits, I get a NullReferenceException
:
at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
My question is: how do I properly use task continuations and ConfigureAwait(false)
at the same time?