I just got a completely irrational error when testing my custom validator:

The value for the property ‘Environment’ is not valid.

Pose that I got the enum:

public enum Environment {
    None,
    Test,
    Production
}

I have a validator attribute on my configuration element class that accepts both Environment.Test and Environment.Production but the above error is thrown for a value Environment.None. Fine, but the value Environment.None should not be set for this XML, right?

<Configurations>
<add Environment="Test" Value="value" />
</Configurations>

I found however that the validator got invoked twice, once for the Environment.None and once for the Environment.Test. Apparently, the validator gets invoked on some kind of default value for the first configuration element, doh!

Anyways, enough with me complaining, I just wanted to notify all other poor people to make sure to default your configuration values so that they don’t conflict with your validators.

Simply doing this would fix it (also see the example for integers):

[ConfigurationProperty ("Environment", IsRequired = true, IsKey = true, DefaultValue = Environment.Test)]
[EnvironmentConfigurationValidator]
public Environment Environment {
    get { return (DeliveryState) base["Environment"]; }
}

[ConfigurationProperty ("Integer", IsRequired = true, DefaultValue = 15)]
[IntegerValidator (MinValue = 15)]
public int Integer {
    get { return (int) base["Integer"]; }
}

Now for more gritting of teeth..