覆盖默认 asp.net 核心取消令牌或更改请求的默认超时

override default asp.net core cancelationtoken or change default timeout for requests

在我的 asp.net 核心 5.0 应用程序中,我调用了一个可能需要一些时间来处理的异步方法。

 await someObject.LongRunningProcess(cancelationToken);

但是,我希望该方法在 5 秒后超时。我知道我可以使用“CancellationTokenSource”来代替 asp.net 核心操作传递的“cancellationToken”:

var s_cts = new CancellationTokenSource();
s_cts.CancelAfter(TimeSpan.FromSeconds(5);
    
await someObject.LongRunningProcess(s_cts );

是否可以将“CancellationTokenSource”用作所有 asp.net 核心请求的默认“取消令牌”策略?我的意思是覆盖作为操作参数传递的那个?

或者是否可以更改 asp.net 核心 5.0 中所有请求的默认超时?

[更新]

解决此问题的一种方法是将该逻辑包装在 class 中。编写一个 class 运行 具有可配置超时的任务。

然后在 DI 中注册它,然后在任何你想重用配置的地方使用它。

public class TimeoutRunner
{
    private TimeoutRunnerOptions _options;

    public TimeoutRunner(IOptions<TimeoutRunnerOptions> options)
    {
        _options = options.Value;
    }

    public async Task<T> RunAsync<T>(Func<CancellationToken, Task<T>> runnable,
                                     CancellationToken cancellationToken = default)
    {
        // cancel the task as soon as one of the tokens is set
        var timeoutCts = new CancellationTokenSource();
        var token = timeoutCts.Token;
        if (cancellationToken != default)
        {
            timeoutCts.CancelAfter(_options.Timeout);
            var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);
            token = combinedCts.Token;
        }

        return await runnable(token);
    }
}

internal static class ServiceCollectionExtensions
{
    public static IServiceCollection AddTimeoutRunner(this IServiceCollection services,
                                                      Action<TimeoutRunnerOptions> configure = null)
    {
        if (configure != null)
        {
            services.Configure<TimeoutRunnerOptions>(configure);
        }

        return services.AddTransient<TimeoutRunner>();
    }
}

public class TimeoutRunnerOptions
{
    public int TimeoutSeconds { get; set; } = 10;
    public TimeSpan Timeout => TimeSpan.FromSeconds(TimeoutSeconds);
}

然后你会在 Startup class,

中注册它
public void ConfigureServices(IServiceCollection services)
{
    services.AddTimeoutRunner(options =>
    {
        options.TimeoutSeconds = 10;
    });
}

然后在需要该全局选项的任何地方使用它:

public class MyController : ControllerBase
{
    private TimeoutRunner _timeoutRunner;

    public MyController(TimeoutRunner timeoutRunner)
    {
        _timeoutRunner = timeoutRunner;
    }

    public async Task<IActionResult> DoSomething(CancellationToken cancellationToken)
    {
        await _timeoutRunner.RunAsync(
            async (CancellationToken token) => {
                await Task.Delay(TimeSpan.FromSeconds(20), token);
            },
            cancellationToken
        );
        return Ok();
    }
}

运行 每个动作调度前的任务

方法 1:动作过滤器

我们可以使用操作过滤器来 运行 一个任务 before/after 每个请求。

public class ApiCallWithTimeeotActionFilter : IAsyncActionFilter
{
    private TimeoutRunner _runner;

    public ApiCallWithTimeeotActionFilter(TimeoutRunner runner)
    {
        _runner = runner;
    }

    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        var result = await _runner.RunAsync(
            async (CancellationToken token) =>
            {
                await Task.Delay(TimeSpan.FromSeconds(20), token);
                return 42;
            },
            default
        );
        await next();
    }
}

然后使用它用 [TypeFilter(typeof(MyAction))] 注释 class:

[TypeFilter(typeof(ApiCallWithTimeeotActionFilter))]
public class MyController : ControllerBase { /* ... */ }

方法二:中间件

另一种选择是使用中间件

class ApiCallTimeoutMiddleware
{
    private TimeoutRunner _runner;

    public ApiCallTimeoutMiddleware(TimeoutRunner runner)
    {
        _runner = runner;
    }

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        // run a task before every request
        var result = await _runner.RunAsync(
            async (CancellationToken token) =>
            {
                await Task.Delay(TimeSpan.FromSeconds(20), token);
                return 42;
            },
            default
        );
        await next(context);
    }
}

然后在Startup.Configure方法中附加中间件:

public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<ApiCallTimeoutMiddleware>();
    app.UseRouting();
    app.UseEndpoints(e => e.MapControllers());
}

自定义 CancellationToken 传递给动作

您需要替换将 HttpContext.RequestAborted 令牌绑定到 CancellationToken 操作参数的默认 CancellationTokenModelBinderProvider

这涉及创建自定义 IModelBinderProvider。然后我们就可以把默认的绑定结果替换成自己的了。

public class TimeoutCancellationTokenModelBinderProvider : IModelBinderProvider
{
    public IModelBinder? GetBinder(ModelBinderProviderContext context)
    {
        if (context?.Metadata.ModelType != typeof(CancellationToken))
        {
            return null;
        }

        var config = context.Services.GetRequiredService<IOptions<TimeoutOptions>>().Value;
        return new TimeoutCancellationTokenModelBinder(config);
    }

    private class TimeoutCancellationTokenModelBinder : CancellationTokenModelBinder, IModelBinder
    {
        private readonly TimeoutOptions _options;

        public TimeoutCancellationTokenModelBinder(TimeoutOptions options)
        {
            _options = options;
        }

        public new async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            await base.BindModelAsync(bindingContext);
            if (bindingContext.Result.Model is CancellationToken cancellationToken)
            {
                // combine the default token with a timeout
                var timeoutCts = new CancellationTokenSource();
                timeoutCts.CancelAfter(_options.Timeout);
                var combinedCts =
                    CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);
                
                // We need to force boxing now, so we can insert the same reference to the boxed CancellationToken
                // in both the ValidationState and ModelBindingResult.
                //
                // DO NOT simplify this code by removing the cast.
                var model = (object)combinedCts.Token;
                bindingContext.ValidationState.Clear();
                bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true });
                bindingContext.Result = ModelBindingResult.Success(model);
            }
        }
    }
}

class TimeoutOptions
{
    public int TimeoutSeconds { get; set; } = 30; // seconds
    public TimeSpan Timeout => TimeSpan.FromSeconds(TimeoutSeconds);
}

然后将此提供程序添加到 Mvc 的默认活页夹提供程序列表中。它需要 运行 在所有其他之前,所以我们将它插入开头。

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.Configure<MvcOptions>(options =>
    {
        options.ModelBinderProviders.RemoveType<CancellationTokenModelBinderProvider>();
        options.ModelBinderProviders.Insert(0, new TimeoutCancellationTokenModelBinderProvider());
    });
    // remember to set the default timeout
    services.Configure<TimeoutOptions>(configuration => { configuration.TimeoutSeconds = 2; });
}

现在 ASP.NET Core 会在看到 CancellationToken 类型的参数时 运行 你的绑定器,它结合了 HttpContext.RequestAborted 令牌和我们的超时令牌。只要其中一个组件被取消(由于超时或请求中止,以先取消者为准),组合令牌就会被触发

[HttpGet("")]
public async Task<IActionResult> Index(CancellationToken cancellationToken)
{
    await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); // throws TaskCanceledException after 2 seconds
    return Ok("hey");
}

参考文献: