SORU
3 Mart 2014, PAZARTESİ


ASP.NET Web tarayıcı isteğini iptal ettiğinde OperationCanceledException API

Bir kullanıcı bir sayfa yüklendiğinde, ASP.NET Web API 2 denetleyicileri isabet eden bir veya birden fazla ajax isteği yapar. Eğer kullanıcı başka bir sayfaya gider, bu ajax istekleri tamamlamadan önce, istekleri, tarayıcı tarafından iptal edildi. ELMAH bizim Http sonra her iptal isteği için: iki hata günlükleri

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

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

Stacktrace, istisna buradan atılan olduğunu görüyorum: 9*

Benim soru: Nasıl ve bu özel durumlar işlemek görmezden gelebilir miyim?

Kullanıcı kodu dışında gibi görünüyor...

Notlar:

  • ASP.NET Web API 2 kullanıyorum
  • Web API uç uyumsuz bir karışımı olan ve olmayan zaman uyumsuz yöntemleri.
  • Hata günlüğü ekliyorum nerede olursa olsun, kullanıcı kodu özel durum yakalamak için kuramıyorum

CEVAP
24 Mart 2014, PAZARTESİ


Bu ASP.NET Web API bir hata değildir 2 ve ne yazık ki, her zaman başarılı olacağına dair bir çözüm olduğunu sanmıyorum. Bizim tarafımızda bunu düzeltmek için bug bulunduk.

Sonuçta, sorun bu durumda ASP.NET iptal bir görev döndürür ve ASP.NET işlenmeyen bir özel durum gibi iptal edilen bir görev (Uygulama durumunda sorun günlüğe kaydeder) davranır.

Bu arada, aşağıdaki kodu gibi bir şey deneyebilirsin. İptali belirteci harekete içeriği kaldırır üst düzey bir ileti işleyicisi ekler. Eğer yanıt hayır içerik varsa, hatayı tetikleyen olmamalı. Hala istemci token iptal denetler ileti işleyicisi hemen sonra çıkarın, çünkü bunlar olabilir küçük bir ihtimal ama üst düzey Web önce API kodu aynı denetleyin. Ancak çoğu durumda yardımcı olacaktır sanırım.

David

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;
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • colacas

    colacas

    29 EKİM 2006
  • DeAdPiXel6667

    DeAdPiXel666

    2 Ocak 2010
  • TomOdellVEVO

    TomOdellVEVO

    29 Mayıs 2012