"Configuration system failed to initialize"
Of you people who have upgraded to the 2.0 framework (or played with it in anger at least) how many of you have seen this error message?
Configuration system failed to initialize.
This has to be the exception that annoys me the most in everything that's changed in 2.0. In 1.1 the most useless exception message was when something went wrong with the XMLSerializer - just some strange error about being unable to load some random dll in some some temp folder.
New in 2.0 is the ConfigurationManager. A new wrapper class that deals with all things {app|web}.config. And the ConfigurationManager is female, so far as I can tell - if things aren't exactly right, then all hell breaks loose, but you have to put in a lot of work to find out exactly what's wrong.
A lot people have had trouble with this over the last few months already, but we got hit with a shocker, simply by forgetting to check one tiny little thing.
Back in the 1.1 framework, all our settings would go in the appSettings node of the app.config. To make it easy (since it was the only thing the developers would touch time and again) we'd put it right at the top, as the first child of the configuration node:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="foobar" value="fnord" />
</appSettings>
...
In 2.0 (in VB) we've now got access to My.Settings. We were in a rush this week, and so rather than work out what our standards should be for using My.Settings, we stuck with just dumping in an appSettings node like we're used to. So as per custom, I stuck it in at the top of the file.
And from that moment on, we could not access any of our settings. If I took it out, our test settings (using My.Settings) would work. Add it in, and neither the appSettings nor My.Settings would work - we'd only get the above error message.
There were two of us working on the project, and I was busy with a few different things at once. After a long while, the other guy found a fix (while I was working on something else) - if the appSettings node was moved lower in the file, then everything came good again.
The next day I had a chance to research what was really going on. Sure, the other guy found a fix, but never actually located the actual problem. It took me five minutes :) If you ever get an exception, and it doesn't tell you enough information, there are sometimes more places to look. All exceptions have an InnerException property - more often than not this value is null, but it isn't, it can be quite useful - in this situation, it was more than enough to identify what's going on.
Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element. (C:\path\to\configfile\WindowsApplication1.exe.config line 6)
Ah ha! So simple once you know. If you ask me however, it's a bloody stupid rule. Why should order matter?
Sometimes these problems are easily solved - sometimes it's not so easy. But my advice is simple: make sure you check _all_ the information available - it might make things a lot easier in the long run.