问题描述
我有一个包含以下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;
属性"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:
属性"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:
要解决此问题,请更改配置属性定义以包括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属性指定范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!