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

2023-11-30

背景:我们正在将 .Net 应用程序迁移到 .Net Core。 作为一项策略,我们希望在完整框架上保持现有功能完整,同时将部分应用程序迁移到 .Net Core。完整的应用程序将支持通过 Net 远程处理和 REST API 的 .services,而 .Net 核心应用程序将仅支持 REST API 服务。

我们决定为整个应用程序保留相同的代码库,并支持在多个平台(NetcoreApp2.1 和 Net472)上编译。 有一个应用程序配置文件。大多数组件都依赖于该文件中存储的信息。因此,我们希望为两个平台保留单个配置文件。 我使用 System.Configuration.ConfigurationManager 包来访问配置信息。

Issue:ConfigurationManager.GetSection(string) 在 .Net core 平台上抛出异常,而在 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>

不幸的是,访问配置有效略有不同 in the 核心框架(以及 .NET 5 和 6)。即使有了下面链接的帮助,我还是花了一些时间才找到答案。 这对我来说是这样的:

作为准备工作,请访问NUGET 包管理器并导入

微软.扩展.配置,
Microsoft.Extensions.Configuration.Json, Microsoft.Extensions.Configuration.Xml
和(可选)Microsoft.Windows.Compatibility

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


应用程序配置

Example:

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

Declare

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

通过初始化它

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

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

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

应用程序配置.json

Example:

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

Declare

 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"];

有用的网址:

  • 无法读取 c-sharp 中的应用程序配置
  • 如何从 json 读取 appsettings 值
  • appSettings 和应用程序设置之间的比较
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

多平台编译:System.Configuration.ConfigurationManager.GetSection 在.NetCore 上抛出错误 的相关文章

随机推荐