【问题标题】:.Net Core 2.0 appsettings with .net full framework using ConfigurationManager.Net Core 2.0 appsettings 与 .net 完整框架使用 ConfigurationManager
【发布时间】:2018-10-19 20:38:08
【问题描述】:

假设项目结构如下:

  1. 项目 A:

    • .net 完整框架(4.6.2)
    • 通过内部访问配置:

    ConfigurationManager.AppSettings["key"];

  2. 项目 B:

    • .net core 2.0(编译成.net 4.6.2)
    • 引用并使用项目A

问题:
.net 核心使用新的配置机制 - 有没有办法连接 .net 核心配置(在项目 B 中),使项目 A 能够通过 ConfigurationManager 使用配置 - 即项目 A 中没有代码更改?

this answer 中添加 NuGet 包 System.Configuration.ConfigurationManager 仅将 ConfigurationManager 添加到项目 B 但在项目 A 中无法通过 ConfigurationManager 进行配置

【问题讨论】:

    标签: c# .net .net-core


    【解决方案1】:

    诀窍是使用 ConfigurationManager.AppSettings.Set 方法从 .NET Core 预填充 ConfigurationManager.AppSettings,以便类库以后可以使用它。

    我正在使用以下类将 json 设置添加到 ConfigurationManager

    public class CustomJsonConfigurationProvider : JsonConfigurationProvider
    {
        public CustomJsonConfigurationProvider(JsonConfigurationSource source) : base(source) { }
    
        public override void Load()
        {
            base.Load();
            foreach (string key in Data.Keys)
            {
                string[] keyParts = key.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                ConfigurationManager.AppSettings.Set(keyParts[keyParts.Length - 1], Data[key]);
            }
        }
    }
    
    public class CustomJsonConfigurationSource : JsonConfigurationSource
    {
        public override IConfigurationProvider Build(IConfigurationBuilder builder)
        {
            FileProvider = FileProvider ?? builder.GetFileProvider();
            return new CustomJsonConfigurationProvider(this);
        }
    }
    
    public static class CustomConfiguratorExtensions
    {
        public static IConfigurationBuilder AddCustomJsonFile(this IConfigurationBuilder builder, string path)
        {
            return AddCustomJsonFile(builder, provider: null, path: path, optional: false, reloadOnChange: false);
        }
    
        public static IConfigurationBuilder AddCustomJsonFile(this IConfigurationBuilder builder, string path, bool optional)
        {
            return AddCustomJsonFile(builder, provider: null, path: path, optional: optional, reloadOnChange: false);
        }
    
        public static IConfigurationBuilder AddCustomJsonFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange)
        {
            return AddCustomJsonFile(builder, provider: null, path: path, optional: optional, reloadOnChange: reloadOnChange);
        }
    
        public static IConfigurationBuilder AddCustomJsonFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange)
        {
            if (provider == null && Path.IsPathRooted(path))
            {
                provider = new PhysicalFileProvider(Path.GetDirectoryName(path));
                path = Path.GetFileName(path);
            }
    
            var source = new CustomJsonConfigurationSource
            {
                FileProvider = provider,
                Path = path,
                Optional = optional,
                ReloadOnChange = reloadOnChange
            };
    
            builder.Add(source);
    
            return builder;
        }
    }
    

    用法:

    builder.ConfigureAppConfiguration((w, c) =>
    {
        c.AddCustomJsonFile("appsettings.json");
    });
    

    【讨论】:

    • 谢谢!所以只是为了澄清一下 - 在您的网络核心应用程序中使用这个类会使您的完整框架中的 ConfigurationManger 类能够使用核心应用程序配置?
    • 是的@JanivZ。我正是在我的应用程序中使用了这个场景,它使用了完整的框架类库。
    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多