我可以在自定义ConfigurationSection上使用IntegerValidator属性指定范围吗? [英] Can I specify a range with the IntegerValidator attribute on a custom ConfigurationSection?
问题描述
我有一个包含以下
namespace DummyConsole {
class TestingComponentSettings: ConfigurationSection {
[ConfigurationProperty("waitForTimeSeconds", IsRequired=true)]
[IntegerValidator(MinValue = 1, MaxValue = 100, ExcludeRange = false)]
public int WaitForTimeSeconds
{
get { return (int)this["waitForTimeSeconds"]; }
set { this["waitForTimeSeconds"] = value; }
}
[ConfigurationProperty("loginPage", IsRequired = true, IsKey=false)]
public string LoginPage
{
get { return (string)this["loginPage"]; }
set { this["loginPage"] = value; }
}
}
}
然后,我的.config文件中包含以下内容:
I then have the following in my .config file:
<configSections>
<section name="TestingComponentSettings"
type="DummyConsole.TestingComponentSettings, DummyConsole"/>
</configSections>
<TestingComponentSettings waitForTimeSeconds="20" loginPage="myPage" />
当我尝试使用此配置部分时,出现以下错误:
When I then attempt to use this configuration section I get the following error:
var Testing = ConfigurationManager.GetSection("TestingComponentSettings")
as TestingComponentSettings;
ConfigurationErrorsException未处理
属性"waitForTimeSeconds"的值无效.错误是:该值必须在1-100的范围内.
The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must be inside the range 1-100.
如果将 IntegerValidator
更改为ExcludeRage = true,我(显然)得到:
If I change the IntegerValidator
to have an ExcludeRage = true, I (obviously) get:
ConfigurationErrorsException未处理
属性"waitForTimeSeconds"的值无效.错误是:该值不能在1-100范围内
The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must not be in the range 1-100
如果我然后将.config中的属性值更改为大于100的数字,则它可以正常工作.
If I then change the value of the property in the .config to a number higher than 100, it works.
如果我更改验证器的 MaxValue
为100,则它可以工作,但也将接受值-1.
If I change the validator to just have a MaxValue
of 100 it works, but will also accept a value of -1.
是否可以在这样的范围内使用 IntegerValidatorAttribute
?
Is it possible to use the IntegerValidatorAttribute
with a range like this?
编辑以添加
由Microsoft确认为问题.
Confirmed as an issue by Microsoft.
推荐答案
Skrud 指出,MS已更新了连接问题:
As Skrud points out, MS have updated the connect issue:
报告的问题是由于配置系统如何处理验证程序而引起的.每个数字配置属性都有一个默认值-即使未指定.如果未指定默认值,则使用值0.在此示例中,配置属性以默认值结束,该默认值不在整数验证器指定的有效范围内.结果,配置解析总是失败.
The reported issue is because of a quirk in how the configuration system handles validators. Each numeric configuration property has a default value - even if one is not specified. When a default is not specified the value 0 is used. In this example the configuration property ends up with a default value that is not in the valid range specified by the integer validator. As a result configuration parsing always fails.
要解决此问题,请更改配置属性定义以包括1到100范围内的默认值:
To fix this, change the configuration property definition to include a default value that is within the range of 1 to 100:
[ConfigurationProperty("waitForTimeSeconds", IsRequired=true,
DefaultValue="10")]
这确实意味着该属性将具有默认值,但是我实际上并不认为这是一个主要问题-我们是说它的值应该在合理的"范围内,并且应该准备好设置合理的默认值.
This does mean that the property will have a default, but I don't actually see that as a major issue - we're saying that it should have a value that falls within a "sensible" range, and should be prepared to set a sensible default.
这篇关于我可以在自定义ConfigurationSection上使用IntegerValidator属性指定范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!