views:

152

answers:

3

1) If ( inside web.config file ) I declare custom section named songPoem before <configSection> , an error is reported saying songPoem element is not recognized. Thus, the following gives me an error:

<songPoem song=”lalala” />

<configSection>
  <section name=”songPoem” type=”A” />
</configSection>

while the following works just fine:

<configSection>
  <section name=”songPoem” type=”A” />
</configSection>

<songPoem song=”lalala” />

A) I assume error is due to .Net reading web.config from top to bottom?! If so, is the order of element declaration an issue only when it comes to custom section elements, or...?

BTW - here's the definition for class A:

public class A: ConfigurationSection
{
   [ConfigurationProperty(“song”)]
     public string Song{ ... } 
}

2) I would assume that only song attribute would be allowed inside <songPoem> element, and thus I would expect that .Net would be able to notice if custom section element includes any non existing attributes. But for some reason I was able to include other attributes also, even though they don’t map to any property of class A:

<songPoem song=”lalala” movie=”this_should_be_here” />

Any idea why Net didn't notice that songPoem contains an invalid attribute?

A: 

well, it uses an xmlreader to read the data, so I would imagine it reads top down, and processes the elements it finds in order.

Though i would suspect it to do a double-pass to support issues like this. I guess it doesn't.

Also, i've always seen a standard practice of declaring config sections at the top, perhaps this is why.

Technically order matters too, because just as with deserialization, i would expect the objects in a list to be deserialized in the same order they were defined.

Brian Rudolph
This is total BS
Janie
why would you say that?
Brian Rudolph
+2  A: 

I did quite an extensive blog post on custom configuration sections a while back to clear up my own confusion about them. That should give you some pointers to help you get it sorted out:

http://www.endswithsaurus.com/2008/11/custom-configuration-section.html

BenAlabaster
I checked your link, but couldn't find anything that would explain with which elements the order of declaration is important. I also couldn't find any explanation on why Net doesn't notice non existing attributes
SourceC
+1  A: 

I've always setup my web.config like so:

<configuration>
    <configSections>
     <sectionGroup name="myConfigGroup">
      ....
     </sectionGroup>
    </configSections>
    <myConfigGroup>
     ....
    </myConfigGroup>
</configuration>

Never had any problems. It truely made me nuts when I made the same mistake as you and couldn't understand what the issue was.

Bryan Migliorisi