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

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

问题描述

背景:
我们正在将.Net应用程序迁移到.Net Core。
作为一种策略,我们希望在Full框架上保持现有功能不变,同时将应用程序的一部分迁移到.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:无法识别的配置节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>


推荐答案

不幸的是,访问配置的工作稍有不同中的核心框架。即使在以下链接的帮助下,我还是花了一些时间才找到它。
这就是它的工作方式:

Unfortunately, accessing configuration works slightly different in the Core Framework. 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"];






有用的链接:

  • cant read app config in c-sharp
  • how to read appsettings values from json
  • Comparision between appSettings and ApplicationSettings

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

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