多平台编译:System.Configuration.ConfigurationManager.GetSection 在 .NetCore 上抛出错误 [英] Multi-platform compilation: System.Configuration.ConfigurationManager.GetSection throws error on .NetCore

查看:31
本文介绍了多平台编译:System.Configuration.ConfigurationManager.GetSection 在 .NetCore 上抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我们正在将 .Net 应用程序迁移到 .Net Core.作为一种策略,我们希望在将部分应用程序迁移到 .Net Core 的同时,在完整框架上保持现有功能不变.完整的应用程序将通过 Net 远程处理和 REST API 支持 .services,而 .Net 核心应用程序将仅支持 REST API 服务.我们决定为整个应用程序保留相同的代码库,并支持在多个平台(NetcoreApp2.1 和 Net472)上编译.有一个应用程序配置文件.大多数组件都依赖于存储在此文件中的信息.因此,我们希望为两个平台保留单个配置文件.我使用 System.Configuration.ConfigurationManager 包来访问配置信息.

Background: We are in the process of migrating .Net application to .Net Core. As a strategy, we would like to keep the existing functionality intact on Full framework while migrating portion of the application to .Net Core. Full application would support .services over Net remoting and REST API whereas .Net core application will only support REST API services. We have decided to keep the same code base for entire application and support compilation on multiple platforms (NetcoreApp2.1 and Net472). There is a single application configuration file. Most of the components are dependent on the information stored in this file. Thus we would like to retain the single configuration file for both platforms. I used System.Configuration.ConfigurationManager package to access configuration information.

问题:ConfigurationManager.GetSection(string) 在 .Net 核心平台上抛出异常,而在 Net472 上运行良好.错误信息:配置系统初始化失败 ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section system.runtime.remoting

Issue: ConfigurationManager.GetSection(string) throws exception on .Net core platform whereas it works fine on Net472. Error Message: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section system.runtime.remoting

目前尝试的解决方法:ConfigurationManager.OpenExeConfiguration(configurationUserLevel).GetSection(string) 在两个平台上都能完美地获取相同的部分

Work around tried so far: ConfigurationManager.OpenExeConfiguration(configurationUserLevel).GetSection(string) works perfect on both the platforms for fetching the same section

示例代码:

static MyConfigurationSection myConfigurationSettings { get; set; }
        static void Main(string[] args)
        {
            LoadSettings();
        }

        private static void LoadSettings()
        {
            try
            {
                //Net472 : Works
                //NetCoreApp2.1: Throws exception
                myConfigurationSettings = ConfigurationManager.GetSection("myCustomSettings") as MyConfigurationSection;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //Works on both platform
            myConfigurationSettings = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).GetSection("myCustomSettings") as MyConfigurationSection;
            Console.WriteLine(myConfigurationSettings.Applications.Count);
            Console.ReadLine();
        }

这里是配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="myCustomSettings" type="TestConfigManager.MyConfigurationSection, TestConfigManager" />    
</configSections>
<myCustomSettings>
<applications/>
</myCustomSettings>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="1111" />
</channels>
</application>
</system.runtime.remoting>
</configuration>

推荐答案

不幸的是,在 Core Framework(以及 .NET 5 和 6).即使在下面链接的帮助下,我也花了一些时间才找到它.这对我来说是这样的:

Unfortunately, accessing configuration works slightly differently in the Core Framework (and also .NET 5 and 6). Even with the help of the links below, it took me some time to find it out. This is how it worked for me:

作为准备,转到NUGET 包管理器并导入

Microsoft.Extensions.Configation,
Microsoft.Extensions.Configation.Json,Microsoft.Extensions.Configation.Xml
和(可选)Microsoft.Windows.Compatibility

Microsoft.Extensions.Configation,
Microsoft.Extensions.Configation.Json, Microsoft.Extensions.Configation.Xml
and (optional) Microsoft.Windows.Compatibility

根据配置文件的类型,按如下方式访问:

Depending on the type of config file, access it as follows:

示例:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="myKey" value="myValue"/>
    </appSettings>
</configuration>

声明

public static AppSettingsSection AppConfig { get; private set; } = null;

通过

AppConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                                .AppSettings;

通过以下方式读取任意键:

var myValue = AppConfig.Settings["myKey"].Value;


appconfig.json

示例:

{
    "AppSettings": {
        "myKey": "myValue"
    }
}

声明

 public static IConfigurationSection JsonConfig { get; private set; } = null;

通过

JsonConfig = new ConfigurationBuilder().AddJsonFile("appconfig.json", 
                optional: true, reloadOnChange: true).Build().GetSection("AppSettings");

通过以下方式读取任意键:

var myValue = JsonConfig["myKey"];


有用的链接:

这篇关于多平台编译:System.Configuration.ConfigurationManager.GetSection 在 .NetCore 上抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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