Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ConfigurationManager is unusable when called from unmanaged code #25027

@Cronan

Description

@Cronan

OS: RH Linux
Framework: .Net Core 2.0

I have a .dll compiled on windows in .net 4.5.2 and written in C#, called from Linux.
I use the code below to inject an app.config file into the system, like so:

var config_proxy = new ConfigurationProxy("my.dll");
config_proxy.InjectToConfigurationManager();

However, when run in Linux, I get the following exception:

"Operation is not supported on this platform."
"   at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
   at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
   at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
   at System.Configuration.ClientConfigurationSystem..ctor()
   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()"

I've looked at the source here, and can see where the throw is, but can't figure out how to get it working ...

public sealed class ConfigurationProxy : IInternalConfigSystem
{
    Configuration config;
    Dictionary<string, IConfigurationSectionHandler> customSections;

   public ConfigurationProxy(string fileName)
    {
        customSections = new Dictionary<string, IConfigurationSectionHandler>();

        if (!Load(fileName))
            throw new ConfigurationErrorsException(string.Format(
                "File: {0} could not be found or was not a valid cofiguration file.",
                config.FilePath));
    }

    private bool Load(string file)
    {
       config = ConfigurationManager.OpenExeConfiguration(file);
       return config.HasFile;
    }

    public Configuration Configuration
    {
        get { return config; }
    }

    #region IInternalConfigSystem Members

    public object GetSection(string configKey)
    {
        if (configKey == "appSettings")
            return BuildAppSettings();

        object sect = config.GetSection(configKey);

        if (customSections.ContainsKey(configKey) && sect != null)
        {
            var xml = new XmlDocument();

            xml.LoadXml(((ConfigurationSection)sect).SectionInformation.GetRawXml());
            sect = customSections[configKey].Create(config,
                config.EvaluationContext,
                xml.FirstChild);
        }

        return sect;
    }

    public void RefreshConfig(string sectionName)
    {
        Load(config.FilePath);
    }

    public bool SupportsUserConfig
    {
        get { return false; }
    }

    #endregion

    private NameValueCollection BuildAppSettings()
    {
        var coll = new NameValueCollection();

        foreach (var key in config.AppSettings.Settings.AllKeys)
            coll.Add(key, config.AppSettings.Settings[key].Value);

        return coll;
    }

    public bool InjectToConfigurationManager()
    {
        var configSystem = typeof(ConfigurationManager).GetField("s_configSystem",
            BindingFlags.Static | BindingFlags.NonPublic);

        configSystem.SetValue(null, this);

        if (ConfigurationManager.AppSettings.Count == config.AppSettings.Settings.Count)
            return true;

        return false;
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions