如何在linux

时间:2017-02-16 18:44:33

标签: linux asp.net-core asp.net-core-mvc

鉴于:

  • ASP.Net Core MVC应用程序,部署在linux上的docker容器中。
  • Nginx反向代理+负载均衡器

当ASP.Net核心容器卡住时(在nginx上超时后),它会调用应用程序的另一个实例。此时,应用程序应取消处理,以便不执行两次数据操作操作。这适用于全局:

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    using (context.HttpContext.RequestAborted.Register(() => context.HttpContext.RequestAborted.ThrowIfCancellationRequested()))
    {
        await base.OnActionExecutionAsync(context, next);
    }
}

问题在于,nginx可能会在控制器中的事务和基础服务完成后立即取消请求。这意味着,请求是成功的,但只是稍微过长,nginx无论如何都会尝试第二次。为了解决这个问题,我们想设置第二个超时,它比nginx超时短几秒。我发现的唯一解决方案是:

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    var timeout = new CancellationTokenSource();

    timeout.CancelAfter(ActionTimeout);

    using (timeout.Token.Register(() => timeout.Token.ThrowIfCancellationRequested()))
    using (context.HttpContext.RequestAborted.Register(() => context.HttpContext.RequestAborted.ThrowIfCancellationRequested()))
    {
        await base.OnActionExecutionAsync(context, next);
    }
}

但这会引发以下异常:

System.AggregateException was unhandled
Message: An unhandled exception of type 'System.AggregateException' occurred in System.Private.CoreLib.ni.dll
Additional information: One or more errors occurred.

然后应用程序崩溃了。将try / catch放在任何地方当然也无济于事,向中间件添加全局异常过滤器也无济于事。

在自定义时间之后取消Linux上的请求的最佳方法是什么,而不依赖于反向代理来执行此操作?

更新 - 有关例外的详细信息:

首先,我看到以下异常:

System.OperationCanceledException was unhandled by user code
  HResult=-2146233029
  Message=The operation was canceled.
  Source=System.Private.CoreLib
  StackTrace:
       at System.Threading.CancellationToken.ThrowOperationCanceledException()
       at ...BaseController.<>c__DisplayClass4_0.<OnActionExecutionAsync>b__0() in ...\Controllers\BaseController.cs:line 28
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  InnerException: 

哪个好,这是取消令牌抛出异常。但第二个,我无法捕捉到的,不知道我怎么能看到那里的细节,我只看到以下屏幕:

Exception Screenshot

无论我怎么做,这个过程总是在之后停止(即使我按下继续执行)

0 个答案:

没有答案