从另一个程序集访问 .NET Core 配置类 [英] Access .NET Core Configuration Class From Another Assembly

查看:24
本文介绍了从另一个程序集访问 .NET Core 配置类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去

在 web.config 文件中,设置可以放在 appSettings 部分,如下所示:

In a web.config file, settings could be placed in an appSettings section like so:

<appSettings>
  <add key="mysetting" value="123"/>
</appSettings>

即使我的 web.config 文件在我的 web 项目中,该项目中使用的任何程序集/库都可以使用以下方法访问设置:

Even though my web.config file was in my web project, any assemblies/libraries used in that project could access the settings using:

ConfigurationManager.AppSettings["mysetting"]

今天(和问题)

我开始使用 .NET 核心,就像以前一样,我的程序集/库本身不是 Web 项目,需要访问各种配置设置.

I am starting to use .NET core, and just like before, I have assemblies/libraries that are not web projects in of themselves and need to access various configuration settings.

Microsoft 的配置文档(https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration) 以及我能找到的所有其他示例,让控制器使用配置类,并且不提供有关如何使其工作的任何指导与另一个不是控制器的程序集/库中的类.

Microsoft's Configuration documentation (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration) along with all of the other examples I can find, have the configuration class being consumed by a controller and don't provide any guidance on how to make it work with a class in another assembly/library that is not a controller.

举一个例子,如果我有一个自定义属性,我可以用它来装饰一个类,并且该自定义属性是在另一个库中(而不是在 Web 项目中)定义的,并且它需要访问配置设置,我该怎么做今天?在这样的实例中,我也无法将任何内容传递给构造函数.

For ONE example, if I have a custom attribute that I can decorate a class with and that custom attribute is defined in another library (not in a web project) and it needs to access a configuration setting, how do I do that today? I can't pass in anything to a constructor in such an instance either.

推荐答案

当您特别提到 web.config 时,我假设您在谈论 ASPNET Core 项目.

I'm going to assume you're talking about a ASPNET Core project as you specifically mention web.config.

这是您需要做的.

IOptions 通常在应用程序启动时配置,这意味着它在运行时可用,代码看起来像这样.

IOptions<FooSettingsClass> is usually configured at application start which means that it's available at runtime with code that looks something like this.

// Adds services required for using options.
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("FooAppSettings"));

最简单的方法是让框架通过构造函数注入它.通常你会看到它(正如你提到的)被注入到控制器中,如下所示:

The easiest way is to have the framework inject it through the constructor. Typically you'll see it (as you mentioned) being injected in the controller like this:

class FooController : Controller {
  public FooController(IOptions<FooSettingsClass> settings) { .
    //.. 
  }
}

如果您需要访问此配置,例如一个服务,那么您只需拥有一个构造函数,在不同的程序集中,它接受这些选项.所以:

If you need to access this configuration is say, a service, then you simply have a constructor, in a different assembly, which accepts those options. So:

public class SomeServiceInAnotherAssembly {
  public SomeServiceInAnotherAssembly(IOptions<FooSettingsClass> settings) {
    //..
  }
}

这显然意味着您的 FooSettingsClass 类需要在您的 ASPNET Core 项目之外(以避免循环依赖),但这是一种无需编写任何代码即可传播配置的方式,这就是我见过其他开发人员这样做.对我来说,编写代码是一个跳圈解决方案,肯定会有错误.

This obviously means that your FooSettingsClass class needs to be outside of your ASPNET Core project (to avoid circular dependencies), but this is one way of propagating your configuration without writing any code, which is what I've seen other developers do. To me, writing code is a hoop jumping solution bound to have bugs.

不要忘记你的类(在这个例子中是SomeServiceInAnotherAssembly)需要在启动时注册,即services.AddScoped();

Don't forget that your class (in this exampe SomeServiceInAnotherAssembly) needs to be registered at startup, i.e. services.AddScoped<SomeServiceInAnotherAssembly>();

这种方法的好处在于它使您的类可测试.

The nice thing about this approach is that it makes your classes testable.

这篇关于从另一个程序集访问 .NET Core 配置类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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