none
Work with app.config in application plugins RRS feed

  • Question

  • Dear all,

    I'm developing application with several plugins which loaded by the reflection. Almost all plugins have a own app.config with different settings which include wcf binding settings.

    Because of reflection, the main application can't process app.config sections from plugin app.configs.

    Maybe anybody can advice me, how i can load sections from plugin app.config to section in main app.config?

    Now, I'm trying to use ConfigurationManager.OpenExeConfiguration. I want to append sections(properties SectionGroups and Sections) from plugin config to config of main application on runtime. But there is a problem. The ConfigurationBindings property(is situated in BindingSection and contains binding descriptions) has only readonly collection and I can't update or append her.

    I'm trying to do the following(at first only for bindings):

      private static void UpdateGeneralConfigByPluginConfig(string pathToLibrary, string appName, Configuration mainAssembly)
            {
                var pluginConfiguration = ConfigurationManager.OpenExeConfiguration(pathToLibrary);
                foreach (ConfigurationSectionGroup sectionGroup in pluginConfiguration.SectionGroups)
                {
                    foreach (ConfigurationSection section in sectionGroup.Sections)
                    {
                        if (section is BindingsSection)
                        {
                            UpdateBindingSection(
                                section as BindingsSection,
                                appName,
                                mainAssembly.SectionGroups[sectionGroup.Name]
                                            .Sections[section.SectionInformation.Name] as BindingsSection);
                        }
                        else {/*...*}
                        }
                    }
                }
            }
       private static void UpdateBindingSection(
                BindingsSection pluginBindingSection,
                BindingsSection mainAssemblyBindingSection)
            {
    
    /*1*/
    
                BindingCollectionElement difference =
                    pluginBindingSection.BindingCollections.Except(mainAssemblyBindingSection.BindingCollections).First();
    
                foreach (var bindingConfigurationElement in difference.ConfiguredBindings)
                {
                    foreach (var bindingCollectionElement in mainAssemblyBindingSection.BindingCollections)
                    {
                        if (bindingCollectionElement.BindingName == difference.BindingName &&
                            bindingCollectionElement.BindingType == difference.BindingType)
                        {
                            bindingCollectionElement.ConfiguredBindings.ToList().Add(bindingConfigurationElement);
                        }
                    }
                }
            }

    In point 1, I found the necessary value from plugin config. The next step, I have to add (or update) it to main config. But I can't do it for readonly collection. The method with .ToList() doesn't work too.

    Maybe there is other variant to implement this logic? or anybody will be able to help me in my variant?

    Thanks in advice.



    • Edited by Denis99 Thursday, October 8, 2015 12:45 PM
    Thursday, October 8, 2015 11:50 AM

Answers