![]() |
睡不着的橡皮擦 · 估值 25 亿美元 ...· 1 年前 · |
![]() |
鼻子大的柑橘 · [摩尔庄园]塞拉剧情整理:为何你哥是黑猫你妹 ...· 1 年前 · |
![]() |
瘦瘦的野马 · 2022广州车展:坦克300赛博骑士正式发布 ...· 1 年前 · |
![]() |
追风的板凳 · 末日大回炉txt本地下载_小红书· 1 年前 · |
![]() |
深情的手术刀 · 沈阳地铁10号线即将载客试运营!这两条线路将 ...· 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();
![]() |
追风的板凳 · 末日大回炉txt本地下载_小红书 1 年前 |