Multi-platform compilation: System.Configuration.C

2020-03-27 09:37发布

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.

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

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

Sample Code:

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();
        }

Here is configuration file

<?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>

1条回答
走好不送
2楼-- · 2020-03-27 10:15

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:

As preparation, go to NUGET package manager and import

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:


App.Config

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;

Initialize it via

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

Read any keys via:

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

appconfig.json

Example:

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

Declare

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

Initialize it via

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

Read any keys via:

var myValue = JsonConfig["myKey"];

Helpful links:

查看更多
登录 后发表回答