ConfigurationProperty Validator
-
Hi I have a question concerning custom ConfigurationValidator, which verifies the entries in the app.config file. Below is the class which represents the section in the config file.
public class SettingSection : ConfigurationSection
{
[ConfigurationProperty("appId", IsKey=true, IsRequired=true), ConfigurationValidator(typeof(AppIdValidator))]
public string AppId
{
get { return (string)base["appId"]; }
}
}public class AppIdValidator : ConfigurationValidatorBase
{
public override bool CanValidate(Type type)
{
return true;
}public override void Validate(object value) { string val = (string)value; Regex regex = new Regex(@"\\A\\d{4}-\\d{4}-\\d{4}-\\d{4}\\z"); if (regex.IsMatch(val)) { return; } else { throw new FormatException("Input string is not in correct format"); }
}
}The problem is when the instance of SettingSection is created, and the validator is called, the metod Validate is called with empty string - no metter what is the value in the app.config file. If I set the DefaultValue of the ConfigurationProperty attribute, than the validator is called with that value - again regardless to the entry in app.config. Any idea will be appreciated Uros