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

查看:131
本文介绍了从另一个程序集访问.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的配置文档(< a href = https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration rel = nofollow noreferrer> 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.

推荐答案

我要假设你在

这是您需要做的。

IOptions< FooSettingsClass> 通常在应用程序启动时进行配置,这意味着它可以在运行时使用如下所示的代码来提供。

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< SomeServiceInAnotherAssembly>();

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天全站免登陆