0

Original question and code: Customized IEnumerable in ConfigurationSection

Advancing in solution to this and with @Daniel Hilgarth answer I have changed (app.config) this:

<section name="Disk"
         type="ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection"/>

to this one:

    <section name="Disk"
             type="ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection,
             ConsoleApplication1_ConfigurationEnumerable"/>

Now, I have this another exception:

System.Configuration.ConfigurationErrorsException was unhandled
  HResult=-2146232062
  Message=Unrecognized element 'Path'. (C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\bin\Debug\ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config line 9)
  Source=System.Configuration
  BareMessage=Unrecognized element 'Path'.
  Filename=C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\bin\Debug\ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config
  Line=9
  StackTrace:
       at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
       at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
       at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
       at System.Configuration.ConfigurationManager.GetSection(String sectionName)
       at ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection.GetConfig() in C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\Disk.cs:line 63
       at ConsoleApplication1_ConfigurationEnumerable.Program.Main(String[] args) in C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\Program.cs:line 9
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Community
  • 1
  • 1
ferpega
  • 3,182
  • 7
  • 45
  • 65
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 07 '13 at 09:25

2 Answers2

1

Try adding the following attribute to your PathsConfigSection.Paths property:

    [ConfigurationCollection(typeof(Paths), AddItemName = "Path")]

It should now look like this:

    [ConfigurationProperty("Paths")]
    [ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
    public Paths Paths
    {
        get
        {
            var o = this["Paths"];
            return o as Paths;
        }
    }
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • @Monkieboy: Thanks. But I am not sure why you repeat my answer. What value do you see in doing that? – Daniel Hilgarth Feb 07 '13 at 09:55
  • I could not fit into a comment and @Ferpega has not marked your answer as complete, I am assuming he has not found the solution. I was using your advice as a platform to provide a solution, I am happy to remove my answer if you feel I am not adding clarity or value to this question. – Mr. Mr. Feb 07 '13 at 09:59
  • @Monkieboy: If you think your answer adds value, leave it be, no problem there. I think Ferpega would have asked for clarification if it still didn't work. You gotta give him some time to test it out - or to even realize that there is an answer ;) – Daniel Hilgarth Feb 07 '13 at 10:00
  • OK, please forgive me I don't want to annoy you or anyone else. I will delete my contribution. – Mr. Mr. Feb 07 '13 at 10:02
  • 1
    @Monkieboy: You certainly didn't annoy anyone and I didn't ask you to delete your answer. Keep cool :-) – Daniel Hilgarth Feb 07 '13 at 10:03
1

Your configuration should look like this now:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Disk" type="ConsoleApplication1.DiskConfigSection, ConsoleApplication1"/>
  </configSections>
  <Disk>
    <Paths>
      <Path name="one" permission="1" />
      <Path name="two" permission="2" />
      <Path name="three" permission="3" />
    </Paths>
  </Disk>
</configuration>

Your classes should now look like this:

using System.Configuration;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        var t = DiskConfigSection.GetConfig();
        // Put a breakpoint on the line after this and put a watch on t
    }
}

public class Path : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get
        {
            return (string)this["name"];
        }
    }

    [ConfigurationProperty("permission", IsRequired = true)]
    public string Permission
    {
        get
        {
            return (string)this["permission"];
        }
    }
}

public class Paths : ConfigurationElementCollection
{
    public Path this[int index]
    {
        get
        {
            return BaseGet(index) as Path;
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new Path();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Path)element).Name;
    }
}

public class DiskConfigSection : ConfigurationSection
{
    public static DiskConfigSection GetConfig()
    {
        var b = ConfigurationManager.GetSection("Disk") != null;
        return b ? (DiskConfigSection)ConfigurationManager.GetSection("Disk") : new DiskConfigSection();
    }

    [ConfigurationProperty("Paths")]
    [ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
    public Paths Paths
    {
        get
        {
            var o = this["Paths"];
            return o as Paths;
        }
    }
}
}

I have tested in a console app and the configuration loads fully.

Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42