ServiceModelEnhancedConfigurationElementCollection.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Diagnostics;
  9. using System.Globalization;
  10. using System.Runtime.Diagnostics;
  11. using System.ServiceModel.Diagnostics;
  12. public abstract class ServiceModelEnhancedConfigurationElementCollection<TConfigurationElement> : ServiceModelConfigurationElementCollection<TConfigurationElement>
  13. where TConfigurationElement : ConfigurationElement, new()
  14. {
  15. internal ServiceModelEnhancedConfigurationElementCollection(string elementName)
  16. : base(ConfigurationElementCollectionType.AddRemoveClearMap, elementName)
  17. {
  18. this.AddElementName = elementName;
  19. }
  20. protected override void BaseAdd(ConfigurationElement element)
  21. {
  22. if (null == element)
  23. {
  24. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  25. }
  26. // Is this a duplicate key?
  27. object newElementKey = this.GetElementKey(element);
  28. if (this.ContainsKey(newElementKey))
  29. {
  30. ConfigurationElement oldElement = this.BaseGet(newElementKey);
  31. if (null != oldElement)
  32. {
  33. // Is oldElement present in the current level of config
  34. // being manipulated (i.e. duplicate in same config file)
  35. if (oldElement.ElementInformation.IsPresent)
  36. {
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  38. SR.GetString(SR.ConfigDuplicateKeyAtSameScope, this.ElementName, newElementKey)));
  39. }
  40. else if (DiagnosticUtility.ShouldTraceWarning)
  41. {
  42. Dictionary<string, string> values = new Dictionary<string, string>(6);
  43. values.Add("ElementName", this.ElementName);
  44. values.Add("Name", newElementKey.ToString());
  45. values.Add("OldElementLocation", oldElement.ElementInformation.Source);
  46. values.Add("OldElementLineNumber", oldElement.ElementInformation.LineNumber.ToString(NumberFormatInfo.CurrentInfo));
  47. values.Add("NewElementLocation", element.ElementInformation.Source);
  48. values.Add("NewElementLineNumber", element.ElementInformation.LineNumber.ToString(NumberFormatInfo.CurrentInfo));
  49. DictionaryTraceRecord traceRecord = new DictionaryTraceRecord(values);
  50. TraceUtility.TraceEvent(TraceEventType.Warning,
  51. TraceCode.OverridingDuplicateConfigurationKey,
  52. SR.GetString(SR.TraceCodeOverridingDuplicateConfigurationKey),
  53. traceRecord,
  54. this,
  55. null);
  56. }
  57. }
  58. }
  59. base.BaseAdd(element);
  60. }
  61. protected override bool ThrowOnDuplicate
  62. {
  63. get { return false; }
  64. }
  65. }
  66. }