2010-01-21 15 views
6

मैं एक क्लास वाली है निम्नलिखित ConfigurationSection:क्या मैं एक कस्टम कॉन्फ़िगरेशनसेक्शन पर IntegerValidator विशेषता के साथ एक सीमा निर्दिष्ट कर सकता हूं?

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 was unhandled

The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must be inside the range 1-100.

मैं चान हैं जीई IntegerValidator, एक ExcludeRage = सच है की मैं (जाहिर है) मिलता है:

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

मैं तो एक नंबर 100 से अधिक करने के लिए .config में संपत्ति के मूल्य बदलते हैं, तो यह काम करता है।

यदि मैं वैधता को बदलता हूं तो केवल MaxValue में से 100 यह काम करता है, लेकिन -1 का मान भी स्वीकार करेगा।

क्या इस तरह की सीमा के साथ IntegerValidatorAttribute का उपयोग करना संभव है? जोड़ने के लिए

संपादित

एक issue by Microsoft के रूप में पुष्टि की है।

+2

माइक्रोसॉफ्ट लिंक को आज समाधान के साथ अपडेट किया गया है। स्पष्ट रूप से यदि कोई डिफ़ॉल्ट मान निर्दिष्ट नहीं है, तो यह डिफ़ॉल्ट मान के रूप में "0" का उपयोग करता है। 0, निश्चित रूप से, सीमा 1-100 के बाहर है। "समाधान" सीमा में मौजूद डिफ़ॉल्ट मान के साथ ConfigProperty विशेषता में DefaultValue = पैरामीटर जोड़ना है। दुर्भाग्यवश इसका मतलब है कि आप एक डिफ़ॉल्ट मान लगा रहे हैं जो शायद आप चाहते हैं/आवश्यकता नहीं हो। मुझे यह समस्या भी हो रही है। खुशी है कि मैं इस सवाल पर ठोकर खाई! – Skrud

उत्तर

13

Skrud के रूप में अंक बाहर, एमएस कनेक्ट मुद्दे को अद्यतन किया है:

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")] 

इसका अर्थ यह है कि संपत्ति एक डिफ़ॉल्ट होगा, लेकिन मैं वास्तव में नहीं दिख रहा है एक प्रमुख मुद्दे के रूप में है कि - हम कर रहे हैं यह कहकर कि यह एक मूल्य होना चाहिए जो "समझदार" सीमा के भीतर आता है, और एक समझदार डिफ़ॉल्ट सेट करने के लिए तैयार होना चाहिए।

+3

यह मेरे लिए काम कर रहा है। मेरे मामले में, मैं विशेष रूप से कॉन्फ़िगरेशन फ़ाइल में निर्दिष्ट विकल्प की आवश्यकता चाहता था, इसलिए मैं डिफ़ॉल्ट मान सेट नहीं करना चाहता था। लेकिन, यह पता चला है कि यदि आप आवश्यक फ़ील्ड को चिह्नित करते हैं तो तथ्य को प्राथमिकता दी जाती है और समय-समय पर फायरिंग से सत्यापन को छोड़कर डिफ़ॉल्ट मान वास्तव में _used_ नहीं होता है। यह थोड़ा सा अंतर्ज्ञानी है, लेकिन यह काम करता है। –

+0

विलियम, धन्यवाद जानना अच्छा है –

संबंधित मुद्दे

 संबंधित मुद्दे