简体   繁体   English

秘密经理交叉图书馆

[英]Secret manager cross library

I am using .Net Core console app with the secret manager. 我正在使用.Net Core控制台应用程序与秘密管理器。 The thing is that I need some of the data contained in the ConfigurationRoot (connectionString for example) to be passed to a library. 问题是我需要将ConfigurationRoot(例如connectionString)中包含的一些数据传递给库。 What I did is create a class on the library with an Static field and populate it on my console app: 我所做的是使用静态字段在库上创建一个类,并在我的控制台应用程序上填充它:

Library: 图书馆:

public class MyLibrary
{
    public static IConfigurationRoot Config;
}

Console App: 控制台应用:

        var builder = new ConfigurationBuilder()
            .SetBasePath(PlatformServices.Default.Application.ApplicationBasePath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddUserSecrets()
            .AddEnvironmentVariables();

        MyLibrary.Config = Configuration;

Is this approach correct or there is a simplest way to do it? 这种方法是正确的还是最简单的方法呢?

You are taking the appropriate approach to loading the appSettings.json upon application start. 您正在采取适当的方法在应用程序启动时加载appSettings.json。

Maybe you could do constructor injection like so: 也许你可以这样做构造函数注入:

public class MyLibrary
{
    private IConfigurationRoot _config;

    public MyLibrary(IConfigurationRoot config)
    {
        _config = config;
    }
}

In your Main()... 在你的Main()......

var builder = new ConfigurationBuilder()
            .SetBasePath(PlatformServices.Default.Application.ApplicationBasePath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddUserSecrets()
            .AddEnvironmentVariables();
var config = builder.Build();

MyLibrary lib = new MyLibrary(config);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM