1

After digging around, I haven't found anything related to this question.

Below is my current code:

Configuration File

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="customSettings" type="CustomAssembly.Configuration.CustomSettingsGroup, CustomAssmbly, Version=1.0.0.0, Culture=neutral">
            <section name="services" type="CustomAssmbly.Configuration.ServiceSection, Agrione.Common45, Version=1.0.0.0, Culture=neutral" requirePermission="false" allowLocation="true" allowDefinition="Everywhere" />
        </sectionGroup>
    </configSections>

    <customSettings>
        <services>
            <service>
                <add name="AgrioneApi"
                         isDefault="true"
                         serviceType="1"
                         url="http://localhost:8094/" />
            </service>
        </services>
    </customSettings>

</configuration>

CustomSettingsGroup

public class CustomSettingsGroup : ConfigurationSectionGroup
{
    [ConfigurationProperty("services", IsDefaultCollection = true)]
    public ServiceSection ServicesSection => Sections["services"] as ServiceSection;
}

ServiceSection Class

public class ServiceSection : ConfigurationSection
{
    [ConfigurationProperty("service", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(BaseConfigCollection<ServiceConfigElement>), AddItemName = "add")]
    public BaseConfigCollection<ServiceConfigElement> Services => (BaseConfigCollection<ServiceConfigElement>)base["service"];

    public ServiceConfigElement GetDefault()
    {
        var result = (from ServiceConfigElement e in Services
                         where e.IsDefault
                         select e).FirstOrDefault() ?? Services[0];

        return result;
    }
}

BaseConfigCollection Class

public class BaseConfigCollection<TConfigElement> : ConfigurationElementCollection
    where TConfigElement : BaseConfigElement, new()
{
    public new TConfigElement this[string name] => (TConfigElement)BaseGet(name);

    public TConfigElement this[int index]
    {
        get => (TConfigElement)BaseGet(index);
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    public void Add(TConfigElement element)
    {
        BaseAdd(element);
    }

    public void Clear()
    {
        BaseClear();
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new TConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TConfigElement)element).Name;
    }

    public void Remove(TConfigElement element)
    {
        BaseRemove(element);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }

    public void Remove(string name)
    {
        BaseRemove(name);
    }
}

What I want to accomplish, is not having, on the configuration file, that "add" in the "service" element. So, I would like to have below configuration file, without the "add".

Any help? Thank you in advance.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="customSettings" type="CustomAssembly.Configuration.CustomSettingsGroup, CustomAssmbly, Version=1.0.0.0, Culture=neutral">
            <section name="services" type="CustomAssmbly.Configuration.ServiceSection, Agrione.Common45, Version=1.0.0.0, Culture=neutral" requirePermission="false" allowLocation="true" allowDefinition="Everywhere" />
        </sectionGroup>
    </configSections>

    <customSettings>
        <services>
            <service name="AgrioneApi"
                         isDefault="true"
                         serviceType="1"
                         url="http://localhost:8094/" />
        </services>
    </customSettings>
</configuration>
Marco Alves
  • 2,776
  • 3
  • 23
  • 33

1 Answers1

0

I think you've got an extra layer in your config based on what you want your end result to be. Rather than looking for a separate service node, you should grab the collection of services:

[ConfigurationProperty("services", IsDefaultCollection = false)]

Then when you look for service elements in the collection you can key on "service" rather than the default "add":

   [ConfigurationCollection(typeof(BaseConfigCollection<ServiceConfigElement>), AddItemName = "service")]

The answer and comments here are similar to what you're doing, I think.

Marco Alves
  • 2,776
  • 3
  • 23
  • 33
tattarrattat
  • 361
  • 2
  • 9
  • Thank you for you answer. On that post there are 5 answers and many comments. Which one should I check? Could you edit your answer on this post and point right to the answer you referring to? – Marco Alves Jun 22 '17 at 21:38
  • The one marked as the answer and its attached comments. The comments discuss the AddItemName parameter. – tattarrattat Jun 22 '17 at 21:41
  • I end up removing the group and initiating it as a section. Worked like a charm! – Marco Alves Jun 23 '17 at 20:44