我可以在自定义 ConfigurationSection 上使用 IntegerValidator 属性指定范围吗?

发布于 2024-08-18 20:19:39 字数 1962 浏览 4 评论 0原文

我有一个包含以下内容的类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-08-25 20:19:39

正如 Skrud 指出的那样,微软已经更新了连接问题:

所报告的问题是由于配置系统处理验证器的方式存在问题所致。每个数字配置属性都有一个默认值 - 即使未指定。如果未指定默认值,则使用值 0。在此示例中,配置属性最终的默认值不在整数验证器指定的有效范围内。结果配置解析总是失败。

要解决此问题,请更改配置属性定义以包含 1 到 100 范围内的默认值:

[ConfigurationProperty("waitForTimeSeconds", IsRequired=true, 
                       默认值=“10”)]

这确实意味着该属性将具有默认值,但我实际上并不认为这是一个主要问题 - 我们'意思是它应该有一个落在“合理”范围内的值,并且应该准备设置一个合理的默认值。

As Skrud points out, MS have updated the connect issue:

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文