.net 执行一个服务器端调用,使用了casec/await模式(错误:TaskCanceledException)

0tdrvxhp  于 2023-11-20  发布在  .NET
关注(0)|答案(2)|浏览(127)

我正在使用WebAPI技术对我的Web API执行AJAX调用。
处理该请求的方法如下:

  1. [Route("RenderNotificationsPopup")]
  2. [HttpGet]
  3. public async Task<string> RenderNotificationsPopup(bool responsive = false)
  4. {
  5. return await GetContentAsync(string.Format("RenderNotificationsPopup?responsive={0}", responsive ? 1 : 0));
  6. }

字符串
此方法调用GetContentAsync,它使用类HttpClient执行服务器端调用。

  1. private async Task<string> GetContentAsync(string urlSuffix)
  2. {
  3. using (var client = new HttpClient())
  4. {
  5. return await client.GetStringAsync(string.Format(
  6. "{0}/Header/{1}",
  7. ConfigurationManager.AppSettings["GatewaysHttpRoot"],
  8. urlSuffix));
  9. }
  10. }


一切正常,除了当用户关闭页面而AJAX查询正在等待来自WebApi的响应时。在这种情况下,我得到这个错误:
消息:系统.线程.任务.任务取消异常:任务被取消.
在System.ThrowForNonSuccess(任务任务)中
在System. System. Data. DataServices.TaskAwaiter. HandleNoncalling和NoncallinggerNotification(任务任务)
在System. System. Data. DataServices.TaskAwaiter`1.GetResult()
在System.Web.Http.Controllers.ApiControllerController.d__0.MoveNext()

  • 从抛出异常的前一个位置开始的堆栈跟踪结束-
    在System. ExceptionServices.ExceptionDispatchInfo.Throw()
    在System.ThrowForNonSuccess(任务任务)中
    在System. System. Data. DataServices.TaskAwaiter. HandleNoncalling和NoncallinggerNotification(任务任务)
    在System. System. Data. DataServices.TaskAwaiter`1.GetResult()
    在System.Web.Http.Controllers.SqlFilterResult.d__2.MoveNext()
  • 从抛出异常的前一个位置开始的堆栈跟踪结束-
    在System. ExceptionServices.ExceptionDispatchInfo.Throw()
    在System.ThrowForNonSuccess(任务任务)中
    在System. System. Data. DataServices.TaskAwaiter. HandleNoncalling和NoncallinggerNotification(任务任务)
    在System. System. Data. DataServices.TaskAwaiter`1.GetResult()
    在System.Web.Http.Controller.ExceptionFilterResult.d__0.MoveNext()
    我可以通过加载页面并在得到AJAX查询的响应之前关闭它来重现这个问题,就像在WebApi处理查询之前离开页面一样。
    我在网上搜索了一下,发现有一个与此相关的bug(ASP.NET Web API OperationCanceledException when browser cancels the request)。尽管如此,它显然涉及旧版本的WebApi包,我使用的是最新版本。
    是我在代码中犯了一个关于等待/等待模式的错误,还是它总是那个讨厌的bug?
    多谢了!

编辑:我尝试了我链接的SO帖子中给出的解决方法(使用类CancelledTaskBugWorkaroundMessageHandler),我仍然得到TaskCanceledException。
编辑2:看来这个问题是无法避免的,请问WebApi应用中是否可以处理此类异常?

cnwbcb6i

cnwbcb6i1#

我使用自己的ExceptionFilterAttribute派生类来记录与WebApi应用程序相关的异常,覆盖方法OnException
我发现处理异常TaskCanceledException的正确方法是覆盖OnExceptionAsync而不是OnException,并依赖属性IsCancellationRequested来检测任务何时被取消。
下面是我使用的错误处理方法:

  1. public override async Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
  2. {
  3. if (!cancellationToken.IsCancellationRequested)
  4. {
  5. //Handle domain specific exceptions here if any.
  6. //Handle all other Web Api (not all, but Web API specific only ) exceptions
  7. Logger.Write(context.Exception, "Email");
  8. context.Response = context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "An unhandled exception was thrown.");
  9. }
  10. }

字符串
其中类Logger来自企业库应用程序块。

展开查看全部
u5rb5r59

u5rb5r592#

如果底层客户端连接断开,则任务被取消是正常的。您需要处理异常。

相关问题