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?
ContinueWith
is what you used prior to async-await
. Await automatically registers the rest of the method as a continueation so you don't need to do that. To achieve what you want with await
, you can register a callback with the CancellationToken
to log when it is cancelled.
CancellationTokenSource cts = new CancellationTokenSource();
cts.Token.Register(() => /* TODO: log */);
var task = _httpClient.SendAsync(request, cts.Token).ConfigureAwait(false);
ConfigureAwait(false)
simply tells the compiler not to switch back to the captured synchronization context. If you used ContinueWith
, then you can supply a TaskContinutationOptions.ExecuteSynchronously
.
var task = _httpClient.SendAsync(request, cts.Token);
return task.ContinueWith(i => { /* TODO: log */ },
TaskContinuationOptions.OnlyOnCanceled | TaskContinutationOptions.ExecuteSynchronously);