dotnet 6和NetStandard 2.0中的配置manager的类似物和受保护的部分

发布于 2025-01-23 19:38:26 字数 1195 浏览 5 评论 0原文

在.NET框架中,我们曾经使用web.config或app.config使用configurationManagerwebconfigurationManager读取配置段。

2个项目引人注目:

a)通过框架加载标准配置文件(XML)。因此,当您调用configurationManager中的任何 dll中的任何 dll中的任何 dll,您会获取通过应用程序域路由的部分或appsettings

b)有能力拥有受保护的部分,即您放置了一些开放的文本数据,并且在运行应用程序时,此数据将被加密。

现在,在.NET 6中,我们有很多关于 new 配置管理器的讨论 - microsoft.extensions.configuration.configurationmanager

首先,.NET Core没有它,但是Microsoft很快添加了system.configuration.configurationManager,现在可用于net6.0 and netStandard2.0

我发现了如何使用microsoft.extensions.configuration.configurationmanager直接从类库中找到零信息。这似乎都是为端点应用程序(执行程序集)设计的。

我似乎明白,如果您真的想要这样的东西,有多种方法,但所有方法似乎都过于令人费解,并且与ASP.NET Core直接相关。最后,不能解决主要问题 - 如何处理DLL的这些受保护的配置。例如,在我的体系结构中,EF的内容位于端点应用程序的2个级别。我不想在下面传递连接字符串。我希望我的EF层本身能够独立地找到加密连接,如果它是从控制台应用程序,测试项目或获胜表格中调用的。

在通常的情况下,所有这些事情是否有任何指导 - “我们现在在ASP.NET核心中拥有此类”?

In the .net framework, we've used to use either web.config or app.config with ConfigurationManager or WebConfigurationManager to read the configuration sections.

2 items stood out:

a) standard configuration file (XML) was loaded by framework. So when you call ConfigurationManager from ANY DLL in your application assemblies you get the section or appSettings that routed through Application Domain.

b) There was ability to have protected sections, i.e. you put some open text data and when application runs, this data will be encrypted.

Now, in .net 6 we have plenty of talk about new configuration manager -Microsoft.Extensions.Configuration.ConfigurationManager.

At first .net core came without it but Microsoft soon added back System.Configuration.ConfigurationManager, now available for net6.0 and netstandard2.0

I have found ZERO information on how to use Microsoft.Extensions.Configuration.ConfigurationManager directly from a class library. It is all seem to be designed for endpoint application, the executing assembly.

I seem understand that if you really want something like this, there are ways of doing it but all of it seem to be overly convoluted and directly related to ASP.NET CORE. And in the end, does not solve the main issue - how to deal with these protected configs from a DLL. For example, in my architecture, the EF stuff is located 2 levels down from the endpoint app. I don't want to pass connection string down below. I want my EF layer to be able by itself find encrypted connection, independently, if it is invoked from a console app, test project or Win Forms.

Are there any guidance to all these things outside the usual - "we now have this class in asp.net core"??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白色秋天 2025-01-30 19:38:26

我来了这个解决方案以实现此功能。它利用单例模式来实现这一目标。同样,这是一个原型。 3个项目

  1. 配置库,包含Singleton的以下代码。此代码以您想要的任何方式加载设置。
public class ConfigSingleton
{
    private static ConfigSingleton _instance;


    static ConfigSingleton()
    {
        _instance = new ConfigSingleton();
        _instance.Init();
    }


    public  static ConfigSingleton Instance { get { return _instance; } }
    public string StringSetting { get; private set; }
    public int IntSetting { get; private set; }
    public bool BoolSetting { get; private set; }
    public int InstanceId { get; private set; }

    private void Init()
    {
        IConfiguration Configuration = new msc.ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile("appsettings.development.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables() 
            //.AddUserSecrets("bbbb",)
            
            //.AddCommandLine(args)
            .Build();
            
        var section = Configuration.GetSection("CustomSettingsSection");
        StringSetting = section.GetValue<string>("StringSetting");
        IntSetting = section.GetValue<int>("NumericSetting");
        BoolSetting = section.GetValue<bool>("BoolSetting");
        InstanceId = Guid.NewGuid().ToString().GetHashCode();
    }

}
  1. 消费图书馆。在这里,事情很简单,
public static class ConfigWrapper
{
    public static ConfigSingleton GetConfig()
    {
        return ConfigSingleton.Instance;
    }
}
  1. 消耗了控制台应用程序,它消耗了上面的库,并证明了我们在设置中加载了同样的单例对象
public static void Main(string[] args)
{
    Console.WriteLine("wrapper config id: " + netstandard20.lib.ConfigWrapper.GetConfig().InstanceId);

    var boolSet = netstandard20.lib.ConfigWrapper.GetConfig().BoolSetting;
    Console.WriteLine("Bool from wrapper: " + boolSet);

    var intSet = netstandard20.lib.ConfigWrapper.GetConfig().IntSetting;
    Console.WriteLine("int from wrapper: " + intSet);

    var strSet = netstandard20.lib.ConfigWrapper.GetConfig().StringSetting;
    Console.WriteLine("string from wrapper: " + strSet);

    Console.WriteLine("= = = = = = = = = = = = = = = = = = = = = =");

    Console.WriteLine("config id: " + netstandard20.lib.config.ConfigSingleton.Instance.InstanceId);

    boolSet = netstandard20.lib.config.ConfigSingleton.Instance.BoolSetting;
    Console.WriteLine("Bool from config: " + boolSet);

    intSet = netstandard20.lib.config.ConfigSingleton.Instance.IntSetting;
    Console.WriteLine("int from config: " + intSet);

    strSet = netstandard20.lib.config.ConfigSingleton.Instance.StringSetting;
    Console.WriteLine("string from config: " + strSet);

    Console.Read();
}

。不知道这是否是一个“好主意”

I came to this solution to achieve this functionality. It utilizes Singleton pattern to make this happen. And again, this is a prototype. 3 Projects

  1. Configuration library, contains the following code for singleton. This code loads settings in any way you want.
public class ConfigSingleton
{
    private static ConfigSingleton _instance;


    static ConfigSingleton()
    {
        _instance = new ConfigSingleton();
        _instance.Init();
    }


    public  static ConfigSingleton Instance { get { return _instance; } }
    public string StringSetting { get; private set; }
    public int IntSetting { get; private set; }
    public bool BoolSetting { get; private set; }
    public int InstanceId { get; private set; }

    private void Init()
    {
        IConfiguration Configuration = new msc.ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile("appsettings.development.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables() 
            //.AddUserSecrets("bbbb",)
            
            //.AddCommandLine(args)
            .Build();
            
        var section = Configuration.GetSection("CustomSettingsSection");
        StringSetting = section.GetValue<string>("StringSetting");
        IntSetting = section.GetValue<int>("NumericSetting");
        BoolSetting = section.GetValue<bool>("BoolSetting");
        InstanceId = Guid.NewGuid().ToString().GetHashCode();
    }

}
  1. Consuming library. Things are simple here
public static class ConfigWrapper
{
    public static ConfigSingleton GetConfig()
    {
        return ConfigSingleton.Instance;
    }
}
  1. Consuming console app, which consumes both libraries above and proves that we have same singleton object loaded with our settings
public static void Main(string[] args)
{
    Console.WriteLine("wrapper config id: " + netstandard20.lib.ConfigWrapper.GetConfig().InstanceId);

    var boolSet = netstandard20.lib.ConfigWrapper.GetConfig().BoolSetting;
    Console.WriteLine("Bool from wrapper: " + boolSet);

    var intSet = netstandard20.lib.ConfigWrapper.GetConfig().IntSetting;
    Console.WriteLine("int from wrapper: " + intSet);

    var strSet = netstandard20.lib.ConfigWrapper.GetConfig().StringSetting;
    Console.WriteLine("string from wrapper: " + strSet);

    Console.WriteLine("= = = = = = = = = = = = = = = = = = = = = =");

    Console.WriteLine("config id: " + netstandard20.lib.config.ConfigSingleton.Instance.InstanceId);

    boolSet = netstandard20.lib.config.ConfigSingleton.Instance.BoolSetting;
    Console.WriteLine("Bool from config: " + boolSet);

    intSet = netstandard20.lib.config.ConfigSingleton.Instance.IntSetting;
    Console.WriteLine("int from config: " + intSet);

    strSet = netstandard20.lib.config.ConfigSingleton.Instance.StringSetting;
    Console.WriteLine("string from config: " + strSet);

    Console.Read();
}

This works. Not sure if this is a "great idea"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文