.Net Core 2.0 appsettings with .net full framework using ConfigurationManager [英] .Net Core 2.0 appsettings with .net full framework using ConfigurationManager

查看:21
本文介绍了.Net Core 2.0 appsettings with .net full framework using ConfigurationManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设项目结构如下:

  1. 项目 A:

  • .net 完整框架 (4.6.2)
  • 通过以下方式在内部访问配置:

ConfigurationManager.AppSettings["key"];

ConfigurationManager.AppSettings["key"];

项目 B:

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

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

The Problem:
.net core uses the new configuration mechanism - is there a way to hook up the .net core config ( in project B) in a way that will enable project A to consume configuration via ConfigurationManager - i.e without code changes in project A ?

添加 NuGet 包 System.Configuration.ConfigurationManager 就像在 这个回答只将 ConfigurationManager 添加到项目 B 但在项目 A 中没有配置可通过 ConfigurationManager

adding NuGet package System.Configuration.ConfigurationManager like in this answer only adds ConfigurationManager to project B but in project A no configuration is available via ConfigurationManager

推荐答案

技巧是使用 ConfigurationManager.AppSettings.Set 方法从 .NET Core 预填充 ConfigurationManager.AppSettings以便类库以后可以使用它.

Trick is to use ConfigurationManager.AppSettings.Set method to prepopulate ConfigurationManager.AppSettings from .NET Core so class libraries can use it later.

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

I am using following classes to add json settings into 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");
});

这篇关于.Net Core 2.0 appsettings with .net full framework using ConfigurationManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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