asp.net webapi的过滤器 ExceptionFilterAttribute 捕获一个异常消息如下:
检测到站点发生错误 "已取消该操作。"
错误跟踪: 在 System.Threading.CancellationToken.ThrowIfCancellationRequested() 在 System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext() --- 引发异常的上一位置中堆栈跟踪的末尾 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext() --- 引发异常的上一位置中堆栈跟踪的末尾 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() --- 引发异常的上一位置中堆栈跟踪的末尾 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 System.Web.Http.Controllers.ExceptionFilterResult.d__0.MoveNext()
这种异常我该怎么处理呢。。。求大神们指点
这是一个apibug;需要如下处理
1.新建一个类CancelledTaskBugWorkaroundMessageHandler
using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Web; namespace Teens.WebApi.Common { /// <summary> /// api bug : System.OperationCanceledException 异常处理 /// </summary> 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; } } }
2.在global的Application_Start中注册
GlobalConfiguration.Configure(i => { i.MessageHandlers.Add(new CancelledTaskBugWorkaroundMessageHandler()); });
try again.
我用了CancelledTaskBugWorkaroundMessageHandler后,还是会进入到ExceptionFilterAttribute
我测试的不会进入到handler直接进golbalerror