This is a bug in ASP.NET Web API 2 and, unfortunately, I do not think that the workaround will always be successful. We fixed a mistake to fix it on our side.
Ultimately, the problem is that in this case we are returning the canceled task for ASP.NET, and ASP.NET treats the canceled task as an unhandled exception (it logs the problem in the application event log).
In the meantime, you can try something like the code below. It adds a top-level message handler that deletes content when the cancel token triggers. If the response has no content, the error should not be triggered. There is still a slight chance that this could happen, as the client can disconnect immediately after the message handler checks the cancellation token, but before the higher-level web API code performs the same check. But I think this will help in most cases.
David
config.MessageHandlers.Add(new CancelledTaskBugWorkaroundMessageHandler()); class CancelledTaskBugWorkaroundMessageHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
dmatson Mar 24 '14 at 22:06 2014-03-24 22:06
source share