.net-core - 在 dot net core 中从中间件配置依赖注入(inject)

标签 .net-core

我有一个调用需要 token 的外部服务的类,这是使用设置类注入(inject)的。

现在,根据请求,我需要使用两个 token 之一。

我想使用中间件来查看传入的请求,然后配置注入(inject)到我的服务中的内容,但不确定这是否可行?

我可以从中间件配置 DI 吗?

最佳答案

我不确定您是否可以从中间件中配置依赖注入(inject),因为 DI 在容器设置困惑期间完成(如果您使用的是 .NET Core 提供的 DI 系统)(看看您的 startup.cs类)。

I have a class that calls an external service which requires a token, and this is injected using the Settings class.

Now, depending on the request, I need to use one of two tokens

您不能获取两个 token 并将它们传递给中间件的构造函数吗?我在创建 OWASP secure headers middleware 时做了类似的事情(将配置对象传递给中间件构造函数) (这是现已完成的多部分系列的第二部分)。

在中间件的 Invoke 任务期间,您能否接受配置并打开在请求中找到的东西?

关于.net-core - 在 dot net core 中从中间件配置依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47317050/

相关文章:

c# - 无法加载文件或程序集 '<project name>, Culture=neutral, PublicKeyToken=null'。有人建议修复吗?

.net-core - "process component pubsub error: init timeout for component pubsub exceeded after 5s"

asp.net-core - 从 VS 2017 发布 .net core 应用程序

c# - .Net 核心 Entity Framework - 鉴别器 TPH

c# - .NET Core SDK 的错误 : Version 5. 0.10 0-alpha1-014696 需要至少版本 16.3.0 的 MSBuild

c++ - 系统.DllNotFoundException : Unable to load DLL with dotnet core

c# - 如何动态获取程序集名称

postgresql - 通过 npgsql 使用 ca 证书连接到 .NET Core 上的 DO Postgres 集群

c# - .Net 5 DateTime.ParseExact 问题

msbuild - 便携库导致: Found conflicts between different versions of the same dependent assembly that could not be resolved