none
ConfigurationElementCollection.ThrowOnDuplicate RRS feed

  • Question

  • Have a MyConfigurationElementCollection of MyConfigurationElement which in turn has a property, say "TheKey" defined as key.
    I expected that if config contains elements with duplicate values of "TheKey" the config manager would throw an exception, but not.
    Checked and found that the MyConfigurationElementCollection.ThrowOnDuplicate is false.

    ThrowOnDuplicate is defined with only a getter, what is the criterium for it to return true?
    (I need an exception or other means to detect duplicate, as now element with duplicate value of "TheKey" property are silently ignored even if other properties are different and that is not acceptable)

    Saturday, December 2, 2017 9:35 PM

All replies

  • Hi EuroEager,

    Thank you for posting here.

    >>ThrowOnDuplicate is defined with only a getter, what is the criterium for it to return true?

    If an attempt to add a duplicate ConfigurationElement to this ConfigurationElementCollection will cause an exception to be thrown.

    For more details, please refer to the MSDN article.

    https://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.throwonduplicate%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 5, 2017 8:28 AM
    Moderator
  • But that is, as I stated initially, exactly what is NOT happening.
    Further that is also why I ask for the criteria for ThrowOnDuplicate beeing true.

    In other words, an exception is not beeing thrown and the ThrowOnDuplicate is false (have checked), but the documentation states nothing about under which conditions ThrowOnDuplicate is actually true.

    Tuesday, December 5, 2017 6:03 PM
  • Hi EurpEager,

    Thank you for feedback.

    Like what said in MSDN article, ConfigurationElementCollection.ThrowOnDuplicate gets a value indicating whether an attempt to add a duplicate ConfigurationElement to the ConfigurationElementCollection will cause an exception to be thrown. If you get true, it will throw an exception.

    Have you check the Remarks in the MSDN article? All the situation below will return true.

    When you add a duplicate element, an exception is thrown if the CollectionType value of the element is either AddRemoveClearMap or AddRemoveClearMapAlternate.

    Note that elements with identical keys and values are not considered duplicates, and are accepted silently. Only elements with identical keys but different values are considered duplicates.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 11, 2017 6:31 AM
    Moderator