【问题标题】:"Task was canceled" errors on Azure websiteAzure 网站上的“任务已取消”错误
【发布时间】:2014-04-25 16:29:22
【问题描述】:

随着我们 Azure 网站的流量增加,我们看到越来越多的“任务已取消”错误。典型的跟踪可能如下所示:

Fatal web api error: Controller: CustomerUserEventsController; 
Url: http://app.payboard.com/api/organizations/9ddf55d1-e0c1-4a8f-9327-eef38682e090/addcustomeruserevent?callback=jQuery210035782216349616647_1398442710964&cookieId=05be2755-dc0d-414d-b0d2-ea1986a929c3&customerId=&customerName=&customerUserId=&customerUserFirstName=&customerUserLastName=&eventName=hr-index-GET&_=1398442710965; 
Error: A task was canceled. ( at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
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.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.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext())

我们过去可能每天得到一个;它一直在逐渐增加,所以现在我们得到了几十个。

您会注意到堆栈跟踪中只有框架代码 - 根本没有我们的代码。所以我对如何解决它有点茫然。有关如何跟踪这些问题的任何建议?

【问题讨论】:

  • 我们之前也遇到过这个问题,当服务器非常忙于处理请求或当时不可用时会发生该异常。我们所做的是横向扩展 Azure 网站,至少现在我们不再看到此异常。但我不太确定。

标签: c# azure asynchronous async-await azure-web-app-service


【解决方案1】:

在这里查看答案:

ASP.NET Web API OperationCanceledException when browser cancels the request

还有这里的 WebAPI 错误:

http://aspnetwebstack.codeplex.com/workitem/1797

基本上,当一个 HTTP 请求被取消时,例如,如果用户关闭发起请求的页面,这是预期的。为了防止它出现在您的日志中,基本上,您只需要创建一个自定义消息处理程序来抑制错误:

/// <summary>
/// See https://stackoverflow.com/questions/22157596/asp-net-web-api-operationcanceledexception-when-browser-cancels-the-request
/// </summary>
public class CancelledTaskBugWorkaroundMessageHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var 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】:

    遇到了完全相同的问题。在我的情况下,解决方案是将权限(https URL)从 https://login.windows.net/... 更改为 https://login.microsoftonline.com/... 因为它无法再访问。我没有公布全部授权,因为它是我们组织的特定授权。理论上第一个更直接,所以我根据微软专家的建议使用了它。但如果达不到,那就有问题了。需要弄清楚为什么它无法访问。

    希望这将有助于解决您的问题。 问候, 科恩。

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2016-03-18
      • 2021-07-30
      • 1970-01-01
      相关资源
      最近更新 更多