[英]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放入其自己的程序集中,以允许所有层对其进行访问。
Edit: 编辑:
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.