2

I am working on a task system where i want some process isolation, for which I am spinning up a child console app for executing some unknown code. Basically the child process / isolation part I want opaque via-a-vis the parent app / process. I am trying to get the same environment setup in the child process as the parent - the last pain with that is the app.config.

Ideally I'd like the parent process app.config to be read / available in the child console app. To that end, the best 2 ways I've found so far:

Change default app.config at runtime

This probably works (haven't tested) - but I am a bit uncomfortable with this since this is touching fixed internals.

And the code I've got below:

private static void LoadConfig(string configPath)
{
    var targetConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = configPath }, ConfigurationUserLevel.None);

    var sectionList = targetConfig.Sections.Keys.Cast<string>();

    foreach (ConfigurationSectionGroup part in targetConfig.SectionGroups)
    {
        sectionList = sectionList.Concat(part.Sections.Keys.Cast<string>().Select(k => part.Name + "/" + k));  
    }

    targetConfig.SaveAs(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath, ConfigurationSaveMode.Full);

    foreach (var x in sectionList)
    {
        ConfigurationManager.RefreshSection(x);
    }
}

This works for me as well - since I can happily over-write the client console config file. The pain is the "stupid" .RefreshSection() method - it only refreshes fully-named / specific sections. For example - if I have a system.serviceModel entry .RefreshSection(system.serviceModel) doesn't work - just silently passes! It needs .RefreshSection(system.serviceModel/client), and all such sections with a full path, so I'd have to traverse the entire SectionGroup tree and build paths for all sections (I am only doing it in the above code for 1 level) and refresh each individually - which seems stupidly tedious.

Any better approach I could take? or something I am missing / messing up?

Community
  • 1
  • 1
Vivek
  • 2,103
  • 17
  • 26

0 Answers0