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