zaterdag 18 februari 2006

A 'problem' with custom configSections in .NET 2.0

Yesterday evening, I was trying to port a simple .NET 1.1 test project of mine to .NET 2.0.
This project contained an App.Config file that contains a custom configuration section like this:
<configuration>
<configSections>
<section name="test"
type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<test>
<add key="xx" value="yy"/>
</test>
</configuration>


When I compiled my project, I got the following error message from VS.NET:

Could not find schema information for the element test
Could not find schema information for the element add
Could not find schema information for the element key
Could not find schema information for the element value

I was really surprised... This just worked in .NET 1.1, why shouldn't it work in .NET 2.0 ?
I spent a lot of time searching on Google, newsgroups, etc... for more information about this error and how to solve it, but I haven't found any clue.

This morning, I restarted my quest, and I was able to solve the problem.
Apparently, I had a syntax error somewhere in my code. In the configuration file, there was nothing wrong. Once this syntax error was solved, VS.NET didn't complain anymore.
I tried the same thing in my .NET 1.1 project: I added a syntax error in my code, and compiled. The compiler complained about the syntax error, but not about the configuration file (which is good, because the config file is correct).

This is very frustrating. Why is VS.NET 2005 complaining about something in the configuration file, while the configuration file is just correct ?
On top of it, this 'configuration file error' appears as the first error in my error-list.

I really lost a lot of time due to this strange behaviour of VS.NET; why is VS.NET 2005 behaving like that, which is incorrect IMHO, while VS.NET 2003 is acting like it should.
This error is very confusing and it can really take your productivity down.

Update: I've been playing with configuration-settings in .NET 2.0 again, and again, I came across this annoying error. I've made 2 comments on this article that should provide a way to fix this.

2 opmerkingen:

Frederik Gheysels zei

I am playing a bit with the new Configuration classes in .NET 2.0, and again, I ran into this stupid 'could not find schema information ... ' warning.
This is very annoying, and since I treat my warnings as errors, it is even more annoying.
So, I crawled a bit on the Internet, and I've found this post by Peter Ritchie, which was very helpfull in my situation:
Silly config file warnings

Peter Ritchie has created an updated version of the DotNetConfig.xsd file. I've replaced the one that was installed by VS.NET by this updated version, and now it works like a charm.

Frederik Gheysels zei

If you still have those 'information messages' saying, 'Cannot find schema information for ...', you can make them dissappear:
You'll have create your own XSD that describes your custom configuration section, and save it to the
\Program Files\Microsoft Visual Studio 8\Schemas\
directory.
Then, in the DotNetConfig.xsd file (which is located in that directory as well), you'll have to include the schema-file (xsd) you've just created, by adding this line:
<xs:include schemaLocation="mySchemaFile.xsd" />