TypeInitializationException when using [IntegerValidator] with a [ConfigurationProperty]

by Dominic Zukiewicz 26. February 2010 15:14

I was writing my own custom configuration class to reduce the amount of <appSettings> in my code, but also to make sure the values were type safe. I wrote a simple test application to try this out.

   1: class MyConfigurationTestConfiguration : System.Configuration.ConfigurationSection
   2: {
   3:     private static MyConfigurationTestConfiguration settings = ConfigurationManager.GetSection("MyConfigurationSettingsConfiguration") as MyConfigurationTestConfiguration;
   4:  
   5:     public static MyConfigurationTestConfiguration Settings
   6:     {
   7:         get { return settings; }
   8:     }
   9:  
  10:     [ConfigurationProperty("timeout",Options=ConfigurationPropertyOptions.IsRequired)]
  11:     [IntegerValidator(MinValue=1)]
  12:     public int Timeout
  13:     {
  14:         get { return (int)this["timeout"]; }
  15:     }
  16: }

Now, my thinking is that this Configuration class I have created will make sure that the value in my configuration file is of an integer type and that it has a minimum value of 1. So I’ve created an App.config:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:   <configSections>
   4:     <section name="MyConfigurationSettingsConfiguration" 
   5:              type="MyConfigurationTest.MyConfigurationTestConfiguration, MyConfigurationTest"/>
   6:   </configSections>
   7:  
   8:   <MyConfigurationSettingsConfiguration timeout="100"/>
   9: </configuration>

Okay, so my configuration file knows I’m making a custom configuration section and that its layout is represented by my MyConfigurationTestConfiguration class. Now, this seems okay, lets give it a test run.

Hmm – I get an error ….

System.TypeInitializationException: The type initializer for 'MyConfigurationTest.MyConfigurationTestConfiguration' threw an exception.
---> System.Configuration.ConfigurationErrorsException: The value for the property 'timeout' is not valid. The error is: The value must be inside the range 1-2147483647. 
---> System.ArgumentException: The value must be inside the range 1-2147483647.

Well that is odd, as it definitely is 100. The System.TypeInitializationException can be a bit misleading - in fact all of it is! :-)

The problem is that the base ConfigurationSection class is initialising the Timeout value to one that is actually outside of the range of allowable values, making it instantly invalid. Therefore a default value needs to be set up, which is valid and appears in the range on allowed values. This is done in the [ConfigurationProperty].

[ConfigurationProperty("timeout", DefaultValue = (int)1, Options = ConfigurationPropertyOptions.IsRequired)]

This will then pass validation and the value returned.

Tags: , ,

C#

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar