通过继承ConfigurationValidatorBase类,用户可以自定义一个配置系统的验证器,可是今天使用这种方法发现了一个问题:每个ConfigurationProperty在验证其实际值前,都要对其默认值先进行一次验证,注意这里只是验证一次,发生在验证第一个ConfigurationProperty的值之前。表现形式就好像静态构造函数似的。
比如下面的ConfigurationSection和ConfigurationValidatorBase代码:(代码模拟验证年龄必须大于等于15岁,否则抛出异常)
//自定义ConfigurationSection
class AgeSection : ConfigurationSection
{
[ConfigurationProperty("age"), Age15Validator]
public int Age
{
get { return (int)this["age"]; }
set { this["age"] = value; }
}
}
//自定义验证器
class Age15Validator : ConfigurationValidatorBase
{
public override bool CanValidate(Type type)
{
return type == typeof(Int32);
}
public override void Validate(object value)
{
int age = (int)value;
if (age < 15)
throw new ArgumentException("年龄必须大于等于15岁");
}
}
//验证器Attribute
class Age15ValidatorAttribute : ConfigurationValidatorAttribute
{
public override ConfigurationValidatorBase ValidatorInstance
{
get
{
return new Age15Validator();
}
}
}
接着app.config定义一个合法的和一个非法的(Age小于15的AgeSection)ConfigurationSection
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mysec1" type="Mgen.AgeSection, Mgen" />
<section name="mysec2" type="Mgen.AgeSection, Mgen" />
</configSections>
<mysec1 age="29" />
<mysec2 age="4" />
</configuration>
测试逻辑代码:
try
{
var appconf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var sec1 = appconf.GetSection("mysec1") as AgeSection;
if (sec1 != null)
Console.WriteLine(sec1.Age);
var sec2 = appconf.GetSection("mysec2") as AgeSection;
if (sec2 != null)
Console.WriteLine(sec2.Age);
}
catch (ConfigurationErrorsException ex)
{
Console.WriteLine(ex.Message);
}
这段代码,最初想当然是先输出29,因为mysec1会通过验证,接着接到ConfigurationErrorsException,因为mysec2不会通过验证的。可结果却是连29都没有输出,直接显示没有通过验证的错误信息,检查ConfigurationErrorsException的Line属性竟然是0。
经过调试发现执行验证器(本例中的Age15Validator类)的Validate函数次数是3,而不是最初所期望的2。这三次传入的参数值分别是0,29,4。而这个0就是文章开头提到的一类ConfigurationProperty的默认值,再次强调一下这个默认参数的验证只调用一次,并且是限于任何实际ConfigurationProperty的真实值。
那么,针对上述问题,解决方案是设置ConfigurationProperty的默认值并确保这个默认值能够顺利通过验证器的验证,比如上述程序我们就可以把AgeSection的Age属性的默认值改成15.
[ConfigurationProperty("age", DefaultValue = 15), Age15Validator]
public int Age
{
get { return (int)this["age"]; }
set { this["age"] = value; }
}
这样的话,程序就可以按要求运行了。
29
The value for the property 'age' is not valid. The error is: 年龄必须大于等于15
岁 (E:\My Documents\Visual Studio 2008\Projects\TTC\TTC\bin\Release\Mgen.exe.Con
fig line 9)
第一行输出29(mysec1属性通过验证)
第二行输出错误信息并指出行数。(mysec2没有通过验证)