| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Configuration; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Configuration |
| | | 8 | | { |
| | | 9 | | [ConfigurationCollection(typeof(ExtensionElement), CollectionType = ConfigurationElementCollectionType.BasicMap)] |
| | | 10 | | internal class ExtensionElementCollection : ServiceModelConfigurationElementCollection<ExtensionElement> |
| | | 11 | | { |
| | | 12 | | public ExtensionElementCollection() |
| | 2 | 13 | | : base(ConfigurationElementCollectionType.BasicMap, ConfigurationStrings.Add) |
| | | 14 | | { |
| | 2 | 15 | | } |
| | | 16 | | |
| | | 17 | | internal ExtensionElement GetElementExtension(object key) |
| | | 18 | | { |
| | 17 | 19 | | return (ExtensionElement)BaseGet(key); |
| | | 20 | | } |
| | | 21 | | internal void Add(ConfigurationElement element) |
| | | 22 | | { |
| | 16 | 23 | | BaseAdd(element); |
| | 16 | 24 | | } |
| | | 25 | | |
| | | 26 | | protected override void BaseAdd(ConfigurationElement element) |
| | | 27 | | { |
| | 16 | 28 | | if (!InheritedElementExists((ExtensionElement)element)) |
| | | 29 | | { |
| | 16 | 30 | | EnforceUniqueElement((ExtensionElement)element); |
| | 16 | 31 | | base.BaseAdd(element); |
| | | 32 | | } |
| | 16 | 33 | | } |
| | | 34 | | |
| | | 35 | | protected override void BaseAdd(int index, ConfigurationElement element) |
| | | 36 | | { |
| | 0 | 37 | | if (!InheritedElementExists((ExtensionElement)element)) |
| | | 38 | | { |
| | 0 | 39 | | EnforceUniqueElement((ExtensionElement)element); |
| | 0 | 40 | | base.BaseAdd(index, element); |
| | | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | protected override object GetElementKey(ConfigurationElement element) |
| | | 45 | | { |
| | 381 | 46 | | if (null == element) |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element)); |
| | | 49 | | } |
| | | 50 | | |
| | 381 | 51 | | ExtensionElement configElementKey = (ExtensionElement)element; |
| | 381 | 52 | | return configElementKey.Name; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | internal bool ContainsKey(object key) |
| | | 56 | | { |
| | 33 | 57 | | return BaseGet(key) != null; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | private bool InheritedElementExists(ExtensionElement element) |
| | | 61 | | { |
| | | 62 | | // This is logic from ServiceModelEnhancedConfigurationElementCollection |
| | | 63 | | // The idea is to allow duplicate identical extension definition in different level (i.e. app level and mach |
| | | 64 | | // We however do not allow them on the same level. |
| | | 65 | | // Identical extension is defined by same name and type. |
| | 16 | 66 | | object newElementKey = GetElementKey(element); |
| | 16 | 67 | | if (ContainsKey(newElementKey)) |
| | | 68 | | { |
| | 0 | 69 | | ExtensionElement oldElement = (ExtensionElement)BaseGet(newElementKey); |
| | 0 | 70 | | if (oldElement != null) |
| | | 71 | | { |
| | | 72 | | // Is oldElement present in the different level of original config |
| | | 73 | | // and name/type matching |
| | 0 | 74 | | if (!oldElement.ElementInformation.IsPresent && |
| | 0 | 75 | | element.Type.Equals(oldElement.Type, StringComparison.Ordinal)) |
| | | 76 | | { |
| | 0 | 77 | | return true; |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | 16 | 82 | | return false; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private void EnforceUniqueElement(ExtensionElement element) |
| | | 86 | | { |
| | 146 | 87 | | foreach (ExtensionElement extension in this) |
| | | 88 | | { |
| | 57 | 89 | | if (element.Name.Equals(extension.Name, StringComparison.Ordinal)) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException( |
| | 0 | 92 | | string.Format(SR.ConfigDuplicateExtensionName, element.Name))); |
| | | 93 | | } |
| | | 94 | | |
| | 57 | 95 | | bool foundDuplicateType = false; |
| | 57 | 96 | | if (element.Type.Equals(extension.Type, StringComparison.OrdinalIgnoreCase)) |
| | | 97 | | { |
| | 0 | 98 | | foundDuplicateType = true; |
| | | 99 | | } |
| | 57 | 100 | | else if (element.TypeName.Equals(extension.TypeName, StringComparison.Ordinal)) |
| | | 101 | | { |
| | 0 | 102 | | Type elementType = Type.GetType(element.Type, false); |
| | 0 | 103 | | if (elementType != null && elementType == Type.GetType(extension.Type, false)) |
| | | 104 | | { |
| | 0 | 105 | | foundDuplicateType = true; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | 57 | 109 | | if (foundDuplicateType) |
| | | 110 | | { |
| | 0 | 111 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException( |
| | 0 | 112 | | string.Format(SR.ConfigDuplicateExtensionType, element.Type))); |
| | | 113 | | } |
| | | 114 | | } |
| | 16 | 115 | | } |
| | | 116 | | |
| | | 117 | | protected override bool ThrowOnDuplicate |
| | | 118 | | { |
| | | 119 | | get |
| | | 120 | | { |
| | 16 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |