我可以在自定义 ConfigurationSection 上使用 IntegerValidator 属性指定范围吗?
我有一个包含以下内容的类 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 文件中有以下内容:
<configSections>
<section name="TestingComponentSettings"
type="DummyConsole.TestingComponentSettings, DummyConsole"/>
</configSections>
<TestingComponentSettings waitForTimeSeconds="20" loginPage="myPage" />
当我尝试使用此配置部分时,出现以下错误:
var Testing = ConfigurationManager.GetSection("TestingComponentSettings")
as TestingComponentSettings;
ConfigurationErrorsException 未处理
属性“waitForTimeSeconds”的值无效。错误是:该值必须在 1-100 范围内。
如果我将 IntegerValidator 更改为 ExcludeRage = true,我(显然)得到:
ConfigurationErrorsException 未处理
属性“waitForTimeSeconds”的值无效。错误是:该值不能在1-100范围内
如果我随后将 .config 中的属性值更改为大于 100 的数字,则它可以工作。
如果我将验证器更改为仅将 MaxValue
设为 100,则它可以工作,但也接受 -1 值。
是否可以将 IntegerValidatorAttribute 与这样的范围一起使用?
编辑添加
已确认为 Microsoft 问题< /a>.
I have a class containing the following 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; }
}
}
}
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 was unhandled
The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must be inside the range 1-100.
If I change the IntegerValidator
to have an ExcludeRage = true, I (obviously) get:
ConfigurationErrorsException was unhandled
The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must not be in the range 1-100
If I then change the value of the property in the .config to a number higher than 100, it works.
If I change the validator to just have a MaxValue
of 100 it works, but will also accept a value of -1.
Is it possible to use the IntegerValidatorAttribute
with a range like this?
Edit to add
Confirmed as an issue by Microsoft.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Skrud 指出的那样,微软已经更新了连接问题:
这确实意味着该属性将具有默认值,但我实际上并不认为这是一个主要问题 - 我们'意思是它应该有一个落在“合理”范围内的值,并且应该准备设置一个合理的默认值。
As Skrud points out, MS have updated the connect issue:
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.