使用ConfigurationManager的.net完整框架的.Net Core 2.0应用程序设置 [英] .Net Core 2.0 appsettings with .net full framework using ConfigurationManager

查看:175
本文介绍了使用ConfigurationManager的.net完整框架的.Net Core 2.0应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定以下项目结构:


  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使用配置-即,无需在项目中更改代码一个 ?

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 方法预填充 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");
});

这篇关于使用ConfigurationManager的.net完整框架的.Net Core 2.0应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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