首页 新闻 赞助 找找看

System.OperationCanceledException

0
悬赏园豆:20 [已关闭问题] 关闭于 2018-02-09 17:12

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

 

这种异常我该怎么处理呢。。。求大神们指点

依然照旧的主页 依然照旧 | 初学一级 | 园豆:133
提问于:2018-01-11 10:28
< >
分享
所有回答(2)
0

这是一个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.

野路子程咬金 | 园豆:202 (菜鸟二级) | 2018-01-15 11:58

我用了CancelledTaskBugWorkaroundMessageHandler后,还是会进入到ExceptionFilterAttribute

支持(0) 反对(0) zhujinhu | 园豆:29 (初学一级) | 2019-01-08 19:46
0

我测试的不会进入到handler直接进golbalerror

@宋@ | 园豆:202 (菜鸟二级) | 2019-01-22 20:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册