A developer’s life becomes a lot easier by using configuration files. Configuration files, being XML files, can be changed as needed and developers can use configuration files to change settings without recompiling applications. Administrators can use configuration files to set policies that affect how applications run on their computers.
Managed code can use the classes in the System.Configuration namespace to read settings from the configuration files. When we read through the configuration files, there are certain scenarios which will lead to an System.InvalidcastException
If you have distinct groupings of configurations to be included in the application configuration file, consider creating a custom configuration section. This will allow you to have more organizational structuring around the storage of various settings. Include a custom section by including the configSections element in the configuration file and a sub-element called section that defines the custom section and the handler for reading it.
When we try to make use of a custom configuration file, under certain circumstance, application throws a System.InvalidcastException. Below are two such scenarios and the way to get over them.
As we all know, OpenMappedExeConfiguration gets the custom config file by specifying ExeConfigurationFileMap( with the path of the assembly set) and configuration user level. Here is the MSDN article for reference, http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration.aspx
Now, let’s look in to two such scenarios where accessing custom configuration files might lead us to System.InvalidcastException
Scenario 1: We have a configuration file that tries to access the Section information in the code using GetSection but results in the type cast error while it was accessed.
When the type information was checked, everything looked perfect. And the config file being XML format we might think that we can follow any order of elements that are being used in the config file, The faulting config file looked as shown below,
When we make use of this config file, we will get a System.InvalidcastException.
There is one catch in the above snip which is the root cause of the issue. While DOM (Data Object model) tries to parse the config section, it looks for the type information mentioned in the ConfigSections element, and there is a specific requirement that, this ConfigSections appears just below the Configuration element. CLR XML DOM parser assumes that the configSections element is the first element in the configuration file.
Here is how it should appear,
So, when we the make the necessary changes in the custom config file, the issue gets fixed and we are good to use the config file. You could get more information about this in the MSDN article, http://msdn.microsoft.com/en-us/library/ms228256.aspx
Well, that was simple. But when we miss the basic element order in the config file it becomes a little difficult to locate the fault.
Let’s look at the second scenario which also throws InvalidtypeCastException, but under different circumstances,
Scenario 2: Here is another scenario where the application throws InvalidtypeCastException and this time it’s while accessing <Sectiongroups>. Let’s look in to the sample that leads to the exception,
We try to get the section detail of the custom config file, Read( ) function (with the path where we have the custom config as the parameter) does the work for us.
We get the non-custom config file path by using OpenMappedExeConfiguration of the config file,
Handler is implemented as shown below,
While this handler works fine for a default config file, it results in a System.InvalidCastException when we make use of the custom config file.
As IconfigurationSectionHandler is deprecated [ref: http://msdn.microsoft.com/en-us/library/system.configuration.iconfigurationsectionhandler.aspx], the best way to access the Section group is to follow the following method described below,
The configuration section defines which type is used to de-serialize the XML file, and if you try to cast to an incompatible type you will (as expected) get a casting error.
I was able to get it working by using the suggested method of deriving from ConfigurationSection as IconfigurationSectionHandler is deprecated.
Below is the implementation detail;
Please refer to the MSDN article for more details,http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
Now this works absolutely fine. Hope this helps while working with configuration files.
Software Development Engineer