我有一个具有自定义配置部分的 Windows 服务。在 configSectionHandler 类中,我使用属性上的属性来验证设置,如下所示:
//ProcessingSleepTime Property
[ConfigurationProperty("ProcessingSleepTime", DefaultValue = 1000, IsRequired = false)]
[IntegerValidator(MinValue = 5, MaxValue = 60000)]
public Int32 ProcessingSleepTime
{
get
{
if (this["ProcessingSleepTime"] == null)
return 100;
return (Int32)this["ProcessingSleepTime"];
}
set
{
this["ProcessingSleepTime"] = value;
}
}
如果配置文件中的值验证失败,则会引发 ConfigurationErrorsException。在 Windows 服务中,当它试图启动时会发生这种情况,而且它真的很难看(它提供启动调试器)。我怎样才能优雅地处理这个错误?我尝试将 OnStart 方法包装在 try/catch 中,但没有效果。
谢谢。