I have a windows service that has a custom configuration section. In the configSectionHandler class I am using attributes on the properties to validate the settings like this:
//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;
}
}
If a value in the configuration file fails validation, a ConfigurationErrorsException is thrown. In a windows service this happens as it is trying to start and it's really ugly (it offers to launch the debugger). How can I gracefully handle this error? I tried wrapping the OnStart method in a try/catch but it had no effect.
Thanks.