从容的椰子 · 国家支持!南沙商业航天蓄势待发-广州市南沙区 ...· 5 月前 · |
刚毅的红金鱼 · 乡村教师支月英 托起山里孩子的未来 —中国教育在线· 9 月前 · |
聪明的啤酒 · Latex 表格双栏 单栏_latex ...· 1 年前 · |
八块腹肌的机器猫 · 南京麒麟科创园怎么样? - 知乎· 1 年前 · |
彷徨的酱牛肉 · 高教教师杀害自闭症女儿:曾多次想“送她走”, ...· 1 年前 · |
当用户加载一个页面时,它会发出一个或多个ajax请求,这些请求会命中ASP.NET Web API2控制器。如果用户导航到另一个页面,则在这些ajax请求完成之前,浏览器会取消这些请求。然后,我们的ELMAH HttpModule会为每个取消的请求记录两个错误:
错误1:
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`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`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.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`1.GetResult()
at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()
错误2:
System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.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.Web.Http.WebHost.HttpControllerHandler.<CopyResponseAsync>d__7.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.Web.Http.WebHost.HttpControllerHandler.<ProcessRequestAsyncCore>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.Web.TaskAsyncHelper.EndTask(IAsyncResult ar)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
查看堆栈跟踪,我看到异常从这里抛出: https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http.WebHost/HttpControllerHandler.cs#L413
我的问题是:我如何处理和忽略这些异常?
它似乎在用户代码之外...
备注:
void ErrorLog_Filtering
Web中捕获异常
发布于 2014-03-25 06:06:21
这是ASP.NET Web API2中的一个错误,不幸的是,我不认为有解决办法总是会成功。我们提交了 bug 来解决我们这边的问题。
最终,问题是在这种情况下,我们将取消的任务返回给ASP.NET,ASP.NET将取消的任务视为未处理的异常(它将问题记录在应用程序事件日志中)。
在此期间,您可以尝试类似下面的代码。它添加了一个顶级消息处理程序,该处理程序在触发取消令牌时删除内容。如果响应没有内容,就不应该触发bug。仍然有可能发生这种情况,因为客户端可能在消息处理程序检查取消令牌之后立即断开连接,但在更高级别的Web API代码执行相同的检查之前断开连接。但我认为这在大多数情况下都会有帮助。
大卫
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;
}
发布于 2015-11-19 07:18:38
在为WebApi实现异常记录器时,建议扩展
System.Web.Http.ExceptionHandling.ExceptionLogger
类,而不是创建ExceptionFilter。对于取消的请求,WebApi内部不会调用ExceptionLoggers的Log方法(但是,异常筛选器将获得它们)。这是设计好的。
HttpConfiguration.Services.Add(typeof(IExceptionLogger), myWebApiExceptionLogger);
发布于 2016-12-13 22:39:13
这是针对此问题的另一种解决方法。只需在捕获
OperationCanceledException
的OWIN管道的开头添加一个自定义OWIN中间件
#if !DEBUG
app.Use(async (ctx, next) =>
await next();