多平台编译:System.Configuration.ConfigurationManager.GetSection在.NetCore上引发错误

问题描述 投票:2回答:1

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

问题:ConfigurationManager.GetSection(string)在.Net核心平台上引发异常,而在Net472上运行良好。错误消息:配置系统无法初始化---> System.Configuration.ConfigurationErrorsException:无法识别的配置节system.runtime.remoting

解决方法到目前为止尝试过:ConfigurationManager.OpenExeConfiguration(configurationUserLevel).GetSection(string)在两个平台上都可以完美地用于获取同一部分]

示例代码:

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>
.net-core cross-platform configuration-management .net-remoting
1个回答
0
投票

不幸的是,访问配置的工作原理[[在Core Framework中不同。我花了一些时间才找到它。这就是它的工作方式:

作为准备工作,请转到

NUGET程序包管理器

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

根据配置文件的类型,如下访问它:


App.Config

示例:

<?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"];
© www.soinside.com 2019 - 2024. All rights reserved.