简体   繁体   English

如何从.NET Core应用程序的其他层访问IOptions(或任何设置)?

[英]How can I access IOptions (or any settings) from other tiers of my .NET Core app?

I have successfully tried and used IOptions from within a .NET Core Web API Controller. 我已经在.NET Core Web API控制器内成功尝试并使用了IOptions。

For example, this works for me: 例如,这对我有用:

public MyController(IOptions<MySettings> options)
{
    // I can access these _options from any method in this Controller.
    _options = options.Value;
}

However, I need to access my IOptions from other tiers in my app. 但是,我需要从应用程序的其他层访问IOptions。

For instance, perhaps I have this... 例如,也许我有这个...

Controller --calls--> Business Layer --calls--> Data Access Layer 控制器-呼叫->业务层-呼叫->数据访问层

I really don't want to pass an IOptions parameter down the call chain. 我真的不想在调用链中传递IOptions参数。 I've put MySettings in its own assembly to give all layers access to it. 我已将MySettings放入其自己的程序集中,以允许所有层对其进行访问。

  • Am I approaching this wrong? 我要解决这个错误吗?
  • What is the correct way to do this? 正确的方法是什么?
  • Is there a way to have IOptions automatically injected into the constructor like the Controller? 有没有办法让IOptions像Controller一样自动注入到构造函数中? (If so, how do I instantiate each class where I need this?) (如果是这样,如何在需要的地方实例化每个类?)

Edit: 编辑:

  • I'm not really concerned about dependencies, per-se, but the flow of info through a million "linked" tiers. 我并不是真的担心依赖关系,而是通过一百万个“链接”层的信息流。

Controller(connection string from config) --calls--> Business Layer(connection string) --calls--> Data Access Layer(connection string) 控制器(来自配置的连接字符串)--calls->业务层(连接字符串)--calls->数据访问层(连接字符串)

I just used Connection String as an example. 我仅以连接字符串为例。

Thanks. 谢谢。

You don't have to pass it down the chain. 您不必将其传递给整个链。 When you register your IOptions in Startup.cs its accessible to all your layers. 当您在Startup.cs中注册IOptions时,所有图层都可以访问它。 DI container doesn't care where your IOptions are used for as long as that layer has dependency on IOptions and DI container knows which type to use to instantiate it. 只要该层依赖IOptions,并且DI容器知道要实例化它的类型,DI容器就不会在意IOptions的使用位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在控制台应用程序 .NET Core 中使用 IOptions - Cannot use IOptions in console app .NET Core 如何在 WPF CORE 5 中使用 IOptions? - How do I use IOptions in WPF CORE 5? 如何检查我的应用程序与.net核心的兼容性? - How can I check the compatibility of my app against .net core? 如何使用 .NET 6 访问任何 class 中的密钥? - How can I access my secret key in any class with .NET 6? 如何将 dbUrl、其他常量设置为 .net core 2.2 项目的环境变量,以及如何在我的 .net core 2.2 应用程序中读取这些常量? - How can i set dbUrl, other constants as einvironment variable for .net core 2.2 project and how to read those contants inside my .net core 2.2 app? 使用数据访问层安排3层的dotnet核心应用程序 - Arranging dotnet core app for 3-tiers with data access layer 如何使用 IOptions 等验证 .NET Core 配置中的一系列项目 - How to validate a sequence of items from .NET Core configuration using IOptions etc 净核心IOptions <AppSettings> 采用 - Net Core IOptions<AppSettings> use Asp.Net Core RTM StaticFileMiddleware:如何传递IOptions <StaticFileOptions> ? - Asp.Net Core RTM StaticFileMiddleware: how to pass IOptions<StaticFileOptions>? 如何在 ASP.NET Core 的数据访问代码中访问 HttpContext? - How can I access HttpContext in my data access code in ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM