2014-12-11 17 views
10

Tôi đã nâng cấp lên ASP.NET Web API 5.2.2 và bây giờ thấy ngoại lệ sau trong bản ghi Elmah sản xuất của tôi.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() 

Ngoại lệ dường như là lẻ tẻ, và cho đến nay tôi không thể tái tạo cục bộ. Điều gì có thể kích hoạt ngoại lệ này? Tôi có thể làm gì để sửa chữa hoặc làm việc xung quanh không?

Trả lời

5

này trông rất giống với câu hỏi này: ASP.NET Web API OperationCanceledException when browser cancels the request

Nếu câu trả lời được chấp nhận (https://stackoverflow.com/a/22621596/124165) là một lỗi đã được nộp cho vấn đề này ở đây: http://aspnetwebstack.codeplex.com/workitem/1797

Dưới đây là một đoạn mã từ soures trên cho đang xử lý sự cố:

Trong thời gian chờ đợi, bạn có thể thử một số nội dung như mã bên dưới. Nó thêm trình xử lý tin nhắn cấp cao nhất xóa nội dung khi các mã thông báo hủy kích hoạt . Nếu phản hồi không có nội dung, lỗi sẽ không được kích hoạt. Vẫn còn một khả năng nhỏ có thể xảy ra vì khách hàng có thể ngắt kết nối ngay sau khi trình xử lý tin nhắn kiểm tra mã thông báo hủy nhưng trước khi mã API cấp cao hơn API thực hiện cùng một kiểm tra. Nhưng tôi nghĩ nó sẽ giúp ích trong hầu hết các trường hợp.

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