8

次の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 の範囲内でなければなりません。

IntegerValidatorExcludeRage = trueに変更すると、(明らかに) 次のようになります。

ConfigurationErrorsException が処理されませんでした

プロパティ「waitForTimeSeconds」の値が無効です。エラー: 値は 1 ~ 100 の範囲内であってはなりません

次に、.config のプロパティの値を 100 より大きい数値に変更すると、機能します。

バリデーターを 100 だけに変更するMaxValueと動作しますが、-1 の値も受け入れます。

IntegerValidatorAttributeこのような範囲でを使用することは可能ですか?

編集して追加

Microsoft によって問題として確認されています。

4

1 に答える 1

16

Skrud指摘しているように、MSは接続の問題を更新しました。

報告された問題は、構成システムがバリデーターを処理する方法の癖が原因です。各数値構成プロパティには、指定されていない場合でもデフォルト値があります。デフォルトが指定されていない場合、値0が使用されます。この例では、構成プロパティは、整数バリデーターによって指定された有効範囲内にないデフォルト値で終了します。その結果、構成の解析は常に失敗します。

これを修正するには、構成プロパティの定義を変更して、1から100の範囲内のデフォルト値を含めます。

[ConfigurationProperty("waitForTimeSeconds", IsRequired=true, 
                       DefaultValue="10")]

これは、プロパティにデフォルトがあることを意味しますが、実際にはそれが大きな問題であるとは考えていません。「適切な」範囲内の値が必要であり、賢明なデフォルト。

于 2010-01-27T22:07:28.997 に答える