2014-12-11 11 views
10

Ho eseguito l'aggiornamento a API Web ASP.NET 5.2.2 e ora vedo la seguente eccezione nel registro Elmah di produzione.Web Api TaskCanceledException

System.Threading.Tasks.TaskCanceledException: A task was canceled. 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext() 

L'eccezione sembra essere sporadica e finora non sono stato in grado di riprodurre localmente. Cosa potrebbe innescare questa eccezione? C'è qualcosa che posso fare per sistemare o risolvere?

risposta

5

Questo sembra molto simile a questa domanda: ASP.NET Web API OperationCanceledException when browser cancels the request

Se la risposta accettata (https://stackoverflow.com/a/22621596/124165) era che un bug è stata depositata per questo problema qui: http://aspnetwebstack.codeplex.com/workitem/1797

Ecco un frammento di codice dalle soures di cui sopra per aggirare il problema:

Nel frattempo, è possibile provare qualcosa come il codice qui sotto. Aggiunge un gestore messaggi di livello superiore che rimuove il contenuto quando viene attivato il token di annullamento . Se la risposta non ha contenuto, il bug non deve essere attivato. C'è ancora una piccola possibilità che potrebbe verificarsi , perché il client potrebbe disconnettersi subito dopo che il gestore del messaggio controlla il token di cancellazione ma prima che il codice API Web di livello superiore esegua lo stesso controllo. Ma penso che aiuterà nella maggior parte dei casi.

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); 

     // Try to suppress response content when the cancellation token has fired; ASP.NET will log to the Application event log if there's content in this case. 
     if (cancellationToken.IsCancellationRequested) 
     { 
      return new HttpResponseMessage(HttpStatusCode.InternalServerError); 
     } 

     return response; 
    } 
}