具有CancellationToke的AddScoped依赖项 [英] AddScoped dependency with a CancellationToke

查看:109
本文介绍了具有CancellationToke的AddScoped依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET核心依赖项,适用于每个REST API请求。它通过调用 AddScoped 添加到 Startup.ConfigureServices 中。

I have a .NET core dependency that is scoped to each REST API request. It is added in Startup.ConfigureServices with a call to AddScoped.

我想为此添加取消支持。如果我在任何控制器操作的参数中添加 CancellationToken cancelledToken ,则可以获得一个令牌,如果客户端请求是令牌,该令牌将被取消。然后,我可以将该令牌传递给依赖项上的所有方法。

I want to add cancellation support to this. If I add a CancellationToken cancellationToken to any controller action's parameters I can get a token that is cancelled if the client-side request is. I can then pass that token to all the methods on my dependency.

但是,依赖项的范围仅限于请求,因此将令牌向下传递给操作方法就感觉没有必要-可以我只是以某种方式将 CancellationToken 添加到作用域依赖项?

However, the dependency is scoped to the request, so passing the token down through the action to the methods feels unnecessary - could I just add the CancellationToken to the scoped dependency somehow?

推荐答案


我能以某种方式将CancellationToken添加到作用域依赖项吗?

could I just add the CancellationToken to the scoped dependency somehow?

嗯,从技术上讲,是的。即通过注入 IHttpAccessor 并访问 HttpContext.RequestAborted 属性,该属性通常与传递给控制器​​的取消令牌相同

Well, technically yes. i.e. by injecting IHttpAccessorand accessing HttpContext.RequestAborted property, which is the same cancellation token you usually get passed into the controllers action if defined.

但是实际上不鼓励使用action参数重载,因为在每个控制器动作中,您都可以通过 HttpContext.RequestAborted 并将其包含在控制器操作中有点使令牌公开,即在创建Swagger方案时(至少在2017年是这种情况),其中使用 HttpContext 本身

But using the action parameter overload is actually kinda discouraged as in every controller action you can access the cancellation token via HttpContext.RequestAborted and having it in controllers action kinda makes the token public, i.e. when creating Swagger scheme (at least was the case back in 2017), where as using the HttpContext itself didn't expose it to the public.

唯一的例外似乎是,当使用 Poco Controllers时,不会从 Controller ControllerBase 继承并且不注入 IHttpAccessor 放入此控制器。

The only exception to that seems to be, when using "Poco Controllers" which don't inherit from Controller or ControllerBase and not injecting IHttpAccessor into this controller.

但是将取消令牌注入任意服务会出现问题,因为您对Web框架有严格的依赖( IHttpAccessor / HttpContext )。

But injecting cancellation tokens into arbitrary services is problematic as you get a hard dependency on the web framework (IHttpAccessor/HttpContext).

保持 CancellationToken 方法中的参数,该参数可以有意义的方式取消并使令牌成为可选参数,因此,只有在有请求或可以取消的情况下,才可以传递参数

It's best and cleanest to keep having a CancellationToken parameter on your methods which can be cancelled in a meaningful way and make the token optional, so you can only pass the parameter in situation where you have a request or a situation that can be cancelled

public Task<Result> ProcessSomething(string param1, CancellationToken cancellationToken = default)
{
}

这篇关于具有CancellationToke的AddScoped依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆