Tôi có một lớp chứa sau ConfigurationSection:Tôi có thể chỉ định dải ô có thuộc tính IntegerValidator trên ConfigurationSection tùy chỉnh không?
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; }
}
}
}
sau đó tôi có những điều sau đây trong tập tin .config tôi:
<configSections>
<section name="TestingComponentSettings"
type="DummyConsole.TestingComponentSettings, DummyConsole"/>
</configSections>
<TestingComponentSettings waitForTimeSeconds="20" loginPage="myPage" />
Khi tôi sau đó cố gắng sử dụng phần cấu hình này, tôi nhận được lỗi sau :
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.
Nếu tôi chan ge IntegerValidator
để có một ExcludeRage = true, I (rõ ràng) nhận được:
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
Nếu tôi sau đó thay đổi giá trị của bất động sản trong .config một số cao hơn 100, nó hoạt động.
Nếu tôi thay đổi trình xác thực để chỉ có một số MaxValue
của 100 nó hoạt động, nhưng cũng sẽ chấp nhận giá trị -1.
Có thể sử dụng IntegerValidatorAttribute
với phạm vi như thế này không?
Chỉnh sửa để thêm
Xác nhận như một issue by Microsoft.
Liên kết Microsoft đã được cập nhật hôm nay với giải pháp. Rõ ràng nếu một giá trị mặc định không được chỉ định, nó sử dụng "0" làm giá trị mặc định. 0, tất nhiên, nằm ngoài phạm vi 1-100. "Giải pháp" là thêm một tham số DefaultValue = vào thuộc tính ConfigurationProperty với giá trị mặc định nằm trong phạm vi. Thật không may điều này có nghĩa rằng bạn đang áp đặt một giá trị mặc định mà có thể không phải là những gì bạn muốn/cần. Tôi cũng gặp sự cố này. Rất vui khi tôi tình cờ gặp câu hỏi này! – Skrud