CustomBindingCollectionElement.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Configuration;
  9. using System.Globalization;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Channels;
  12. public sealed partial class CustomBindingCollectionElement : BindingCollectionElement
  13. {
  14. [ConfigurationProperty(ConfigurationStrings.DefaultCollectionName, Options = ConfigurationPropertyOptions.IsDefaultCollection)]
  15. public CustomBindingElementCollection Bindings
  16. {
  17. get { return (CustomBindingElementCollection)base[ConfigurationStrings.DefaultCollectionName]; }
  18. }
  19. public override Type BindingType
  20. {
  21. get { return typeof(CustomBinding); }
  22. }
  23. public override ReadOnlyCollection<IBindingConfigurationElement> ConfiguredBindings
  24. {
  25. get
  26. {
  27. List<IBindingConfigurationElement> configuredBindings = new List<IBindingConfigurationElement>();
  28. foreach (IBindingConfigurationElement configuredBinding in this.Bindings)
  29. {
  30. configuredBindings.Add(configuredBinding);
  31. }
  32. return new ReadOnlyCollection<IBindingConfigurationElement>(configuredBindings);
  33. }
  34. }
  35. public override bool ContainsKey(string name)
  36. {
  37. // This line needed because of the IBindingSection implementation
  38. return this.Bindings.ContainsKey(name);
  39. }
  40. protected internal override Binding GetDefault()
  41. {
  42. return System.Activator.CreateInstance<CustomBinding>();
  43. }
  44. internal static CustomBindingCollectionElement GetBindingCollectionElement()
  45. {
  46. return (CustomBindingCollectionElement)ConfigurationHelpers.GetBindingCollectionElement(ConfigurationStrings.CustomBindingCollectionElementName);
  47. }
  48. bool TryCreateMatchingExtension(BindingElement bindingElement, ExtensionElementCollection collection, bool allowDerivedTypes, string assemblyName, out BindingElementExtensionElement result)
  49. {
  50. result = null;
  51. foreach (ExtensionElement element in collection)
  52. {
  53. BindingElementExtensionElement bindingElementExtension = Activator.CreateInstance(Type.GetType(element.Type, true)) as BindingElementExtensionElement;
  54. if (null == bindingElementExtension)
  55. {
  56. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidExtensionType,
  57. element.Type,
  58. assemblyName,
  59. ConfigurationStrings.BindingElementExtensions)));
  60. }
  61. bool isMatch;
  62. if (allowDerivedTypes)
  63. {
  64. isMatch = bindingElementExtension.BindingElementType.IsAssignableFrom(bindingElement.GetType());
  65. }
  66. else
  67. {
  68. isMatch = bindingElementExtension.BindingElementType.Equals(bindingElement.GetType());
  69. }
  70. if (isMatch)
  71. {
  72. result = bindingElementExtension;
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. protected internal override bool TryAdd(string name, Binding binding, Configuration config)
  79. {
  80. if (String.IsNullOrEmpty(name))
  81. {
  82. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("name");
  83. }
  84. if (null == binding)
  85. {
  86. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
  87. }
  88. if (null == config)
  89. {
  90. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("config");
  91. }
  92. ServiceModelSectionGroup sg = ServiceModelSectionGroup.GetSectionGroup(config);
  93. CustomBindingElementCollection customBindings = sg.Bindings.CustomBinding.Bindings;
  94. CustomBindingElement configElement = new CustomBindingElement(name);
  95. customBindings.Add(configElement);
  96. ExtensionElementCollection collection = sg.Extensions.BindingElementExtensions;
  97. CustomBinding customBinding = (CustomBinding)binding;
  98. foreach (BindingElement bindingElement in customBinding.Elements)
  99. {
  100. BindingElementExtensionElement bindingElementExtension;
  101. bool foundMatch = TryCreateMatchingExtension(bindingElement, collection, false, configElement.CollectionElementBaseType.AssemblyQualifiedName, out bindingElementExtension);
  102. if (!foundMatch)
  103. {
  104. foundMatch = TryCreateMatchingExtension(bindingElement, collection, true, configElement.CollectionElementBaseType.AssemblyQualifiedName, out bindingElementExtension);
  105. }
  106. if (!foundMatch)
  107. {
  108. break;
  109. }
  110. bindingElementExtension.InitializeFrom(bindingElement);
  111. configElement.Add(bindingElementExtension);
  112. }
  113. bool retval = configElement.Count == customBinding.Elements.Count;
  114. if (!retval)
  115. {
  116. customBindings.Remove(configElement);
  117. }
  118. return retval;
  119. }
  120. }
  121. }