internal void UpdateEndpointSections(ContextInformation evaluationContext) { ExtensionElementCollection endpointExtensions = ExtensionsSection.UnsafeLookupCollection(ConfigurationStrings.EndpointExtensions, evaluationContext); // Extension collections are additive only (BasicMap) and do not allow for <clear> // or <remove> tags, nor do they allow for overriding an entry. This allows us // to optimize this to only walk the binding extension collection if the counts // mismatch. if (endpointExtensions.Count != this.properties.Count) { foreach (ExtensionElement endpointExtension in endpointExtensions) { if (null != endpointExtension) { if (!this.properties.Contains(endpointExtension.Name)) { Type extensionType = Type.GetType(endpointExtension.Type, false); if (extensionType == null) { ConfigurationHelpers.TraceExtensionTypeNotFound(endpointExtension); } else { ConfigurationProperty property = new ConfigurationProperty(endpointExtension.Name, extensionType, null, ConfigurationPropertyOptions.None); this.properties.Add(property); } } } } } }
private bool TryCreateMatchingExtension(BindingElement bindingElement, ExtensionElementCollection collection, bool allowDerivedTypes, string assemblyName, out BindingElementExtensionElement result) { result = null; foreach (ExtensionElement element in collection) { bool flag; BindingElementExtensionElement element2 = Activator.CreateInstance(System.Type.GetType(element.Type, true)) as BindingElementExtensionElement; if (element2 == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidExtensionType", new object[] { element.Type, assemblyName, "bindingElementExtensions" }))); } if (allowDerivedTypes) { flag = element2.BindingElementType.IsAssignableFrom(bindingElement.GetType()); } else { flag = element2.BindingElementType.Equals(bindingElement.GetType()); } if (flag) { result = element2; return(true); } } return(false); }
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); }
private string GetEndpointName() { string name = string.Empty; ExtensionElementCollection elements = null; Type type = base.GetType(); elements = ExtensionsSection.UnsafeLookupCollection("endpointExtensions", ConfigurationHelpers.GetEvaluationContext(this)); if (elements == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionCollectionNotFound", new object[] { "endpointExtensions" }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } for (int i = 0; i < elements.Count; i++) { ExtensionElement element = elements[i]; if (element.Type.Equals(type.AssemblyQualifiedName, StringComparison.Ordinal)) { name = element.Name; break; } Type o = Type.GetType(element.Type, false); if ((null != o) && type.Equals(o)) { name = element.Name; break; } } if (string.IsNullOrEmpty(name)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionTypeNotRegisteredInCollection", new object[] { type.AssemblyQualifiedName, "endpointExtensions" }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } return(name); }
internal bool CanAdd(string extensionCollectionName, ContextInformation evaluationContext) { bool retVal = false; ExtensionElementCollection collection = ExtensionsSection.UnsafeLookupCollection(extensionCollectionName, evaluationContext); if (null != collection && collection.Count != 0) { string thisAssemblyQualifiedName = ThisType.AssemblyQualifiedName; string thisTypeName = ExtensionElement.GetTypeName(thisAssemblyQualifiedName); foreach (ExtensionElement extensionElement in collection) { string extensionTypeName = extensionElement.Type; if (extensionTypeName.Equals(thisAssemblyQualifiedName, StringComparison.Ordinal)) { retVal = true; break; } if (extensionElement.TypeName.Equals(thisTypeName, StringComparison.Ordinal)) { Type extensionType = Type.GetType(extensionTypeName, false); if (extensionType != null && extensionType.Equals(ThisType)) { retVal = true; break; } } } if (!retVal && DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.ConfiguredExtensionTypeNotFound, SR.GetString(SR.TraceCodeConfiguredExtensionTypeNotFound), this.CreateCanAddRecord(extensionCollectionName), this, null); } } else if (DiagnosticUtility.ShouldTraceWarning) { int traceCode; string traceDescription; if (collection != null && collection.Count == 0) { traceCode = TraceCode.ExtensionCollectionIsEmpty; traceDescription = SR.GetString(SR.TraceCodeExtensionCollectionIsEmpty); } else { traceCode = TraceCode.ExtensionCollectionDoesNotExist; traceDescription = SR.GetString(SR.TraceCodeExtensionCollectionDoesNotExist); } TraceUtility.TraceEvent(TraceEventType.Warning, traceCode, traceDescription, this.CreateCanAddRecord(extensionCollectionName), this, null); } return(retVal); }
internal static ExtensionElementCollection UnsafeLookupCollection(string collectionName, ContextInformation evaluationContext) { ExtensionElementCollection collection = null; ExtensionsSection extensionsSection = null; if (null != evaluationContext) { extensionsSection = (ExtensionsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.ExtensionsSectionPath); } else { if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.EvaluationContextNotFound, SR.GetString(SR.TraceCodeEvaluationContextNotFound), null, (Exception)null); } extensionsSection = (ExtensionsSection)ConfigurationHelpers.UnsafeGetSection(ConfigurationStrings.ExtensionsSectionPath); } switch (collectionName) { case (ConfigurationStrings.BehaviorExtensions): collection = extensionsSection.BehaviorExtensions; break; case (ConfigurationStrings.BindingElementExtensions): collection = extensionsSection.BindingElementExtensions; break; case (ConfigurationStrings.BindingExtensions): collection = extensionsSection.BindingExtensions; break; case (ConfigurationStrings.EndpointExtensions): collection = extensionsSection.EndpointExtensions; break; default: // LookupCollection built on assumption that collectionName is valid. // This should be protected at the callers site. If assumption is invalid, then // configuration system is in an indeterminate state. Need to stop in a manner that // user code can not capture. Fx.Assert(String.Format(CultureInfo.InvariantCulture, "{0} is not a valid ServiceModelExtensionsSection collection name.", collectionName)); DiagnosticUtility.FailFast(String.Format(CultureInfo.InvariantCulture, "{0} is not a valid ServiceModelExtensionsSection collection name.", collectionName)); break; } return(collection); }
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); }
private string GetConfigurationElementName() { string name = string.Empty; ExtensionElementCollection elements = null; Type thisType = this.ThisType; ContextInformation containingEvaluationContext = this.ContainingEvaluationContext; if (containingEvaluationContext == null) { containingEvaluationContext = ConfigurationHelpers.GetEvaluationContext(this); } if (string.IsNullOrEmpty(this.extensionCollectionName)) { if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, 0x8001b, System.ServiceModel.SR.GetString("TraceCodeExtensionCollectionNameNotFound"), this, (Exception)null); } elements = ExtensionsSection.UnsafeLookupAssociatedCollection(this.ThisType, containingEvaluationContext, out this.extensionCollectionName); } else { elements = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, containingEvaluationContext); } if (elements == null) { if (string.IsNullOrEmpty(this.extensionCollectionName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigNoExtensionCollectionAssociatedWithType", new object[] { thisType.AssemblyQualifiedName }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionCollectionNotFound", new object[] { this.extensionCollectionName }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } for (int i = 0; i < elements.Count; i++) { ExtensionElement element = elements[i]; if (element.Type.Equals(thisType.AssemblyQualifiedName, StringComparison.Ordinal)) { name = element.Name; break; } Type type = Type.GetType(element.Type, false); if ((null != type) && thisType.Equals(type)) { name = element.Name; break; } } if (string.IsNullOrEmpty(name)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionTypeNotRegisteredInCollection", new object[] { thisType.AssemblyQualifiedName, this.extensionCollectionName }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } return(name); }
internal bool CanAdd(string extensionCollectionName, ContextInformation evaluationContext) { bool flag = false; ExtensionElementCollection elements = ExtensionsSection.UnsafeLookupCollection(extensionCollectionName, evaluationContext); if ((elements == null) || (elements.Count == 0)) { if (DiagnosticUtility.ShouldTraceWarning) { int num; string str3; if ((elements != null) && (elements.Count == 0)) { num = 0x8001c; str3 = System.ServiceModel.SR.GetString("TraceCodeExtensionCollectionIsEmpty"); } else { num = 0x8001a; str3 = System.ServiceModel.SR.GetString("TraceCodeExtensionCollectionDoesNotExist"); } TraceUtility.TraceEvent(TraceEventType.Warning, num, str3, this.CreateCanAddRecord(extensionCollectionName), this, null); } return(flag); } string assemblyQualifiedName = this.ThisType.AssemblyQualifiedName; foreach (ExtensionElement element in elements) { string str2 = element.Type; if (str2.Equals(assemblyQualifiedName, StringComparison.Ordinal)) { flag = true; break; } if (assemblyQualifiedName.StartsWith(str2, StringComparison.Ordinal)) { Type type = Type.GetType(str2, false); if ((type != null) && type.Equals(this.ThisType)) { flag = true; break; } } } if (!flag && DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, 0x80017, System.ServiceModel.SR.GetString("TraceCodeConfiguredExtensionTypeNotFound"), this.CreateCanAddRecord(extensionCollectionName), this, null); } return(flag); }
string GetEndpointName() { string configuredSectionName = String.Empty; ExtensionElementCollection collection = null; Type extensionSectionType = this.GetType(); collection = ExtensionsSection.UnsafeLookupCollection(ConfigurationStrings.EndpointExtensions, ConfigurationHelpers.GetEvaluationContext(this)); if (null == collection) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionCollectionNotFound, ConfigurationStrings.EndpointExtensions), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } for (int i = 0; i < collection.Count; i++) { ExtensionElement collectionElement = collection[i]; // Optimize for assembly qualified names. if (collectionElement.Type.Equals(extensionSectionType.AssemblyQualifiedName, StringComparison.Ordinal)) { configuredSectionName = collectionElement.Name; break; } // Check type directly for the case that the extension is registered with something less than // an full assembly qualified name. Type collectionElementType = Type.GetType(collectionElement.Type, false); if (null != collectionElementType && extensionSectionType.Equals(collectionElementType)) { configuredSectionName = collectionElement.Name; break; } } if (String.IsNullOrEmpty(configuredSectionName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionTypeNotRegisteredInCollection, extensionSectionType.AssemblyQualifiedName, ConfigurationStrings.EndpointExtensions), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } return(configuredSectionName); }
internal override BindingElementExtensionElement DeserializeExtensionElement(string elementName, XmlReader reader) { //ExtensionElementCollection extensions = ((ExtensionsSection) EvaluationContext.GetSection ("system.serviceModel/extensions")).BindingElementExtensions; ExtensionElementCollection extensions = ConfigUtil.ExtensionsSection.BindingElementExtensions; ExtensionElement extension = extensions [elementName]; if (extension == null) { throw new ConfigurationErrorsException("Invalid element in configuration. The extension name '" + reader.LocalName + "' is not registered in the collection at system.serviceModel/extensions/bindingElementExtensions"); } BindingElementExtensionElement element = (BindingElementExtensionElement)Activator.CreateInstance(Type.GetType(extension.Type)); element.DeserializeElementInternal(reader, false); return(element); }
private Type GetExtensionType(ContextInformation evaluationContext, string name) { ExtensionElementCollection elements = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, evaluationContext); if (!elements.ContainsKey(name)) { return(null); } ExtensionElement element = elements[name]; Type type = Type.GetType(element.Type, false); if (null == type) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidType", new object[] { element.Type, element.Name }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } return(type); }
protected internal override bool TryAdd(string name, Binding binding, System.Configuration.Configuration config) { if (string.IsNullOrEmpty(name)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("name"); } if (binding == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding"); } if (config == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("config"); } ServiceModelSectionGroup sectionGroup = ServiceModelSectionGroup.GetSectionGroup(config); CustomBindingElementCollection bindings = sectionGroup.Bindings.CustomBinding.Bindings; CustomBindingElement element = new CustomBindingElement(name); bindings.Add(element); ExtensionElementCollection bindingElementExtensions = sectionGroup.Extensions.BindingElementExtensions; CustomBinding binding2 = (CustomBinding)binding; foreach (BindingElement element2 in binding2.Elements) { BindingElementExtensionElement element3; bool flag = this.TryCreateMatchingExtension(element2, bindingElementExtensions, false, element.CollectionElementBaseType.AssemblyQualifiedName, out element3); if (!flag) { flag = this.TryCreateMatchingExtension(element2, bindingElementExtensions, true, element.CollectionElementBaseType.AssemblyQualifiedName, out element3); } if (!flag) { break; } element3.InitializeFrom(element2); element.Add(element3); } bool flag2 = element.Count == binding2.Elements.Count; if (!flag2) { bindings.Remove(element); } return(flag2); }
Type GetExtensionType(ContextInformation evaluationContext, string name) { ExtensionElementCollection collection = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, evaluationContext); if (collection.ContainsKey(name)) { ExtensionElement element = collection[name]; Type elementType = Type.GetType(element.Type, false); if (null == elementType) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidType, element.Type, element.Name), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } return(elementType); } return(null); }
internal void UpdateEndpointSections(ContextInformation evaluationContext) { ExtensionElementCollection elements = ExtensionsSection.UnsafeLookupCollection("endpointExtensions", evaluationContext); if (elements.Count != this.properties.Count) { foreach (ExtensionElement element in elements) { if ((element != null) && !this.properties.Contains(element.Name)) { Type type = Type.GetType(element.Type, false); if (type == null) { ConfigurationHelpers.TraceExtensionTypeNotFound(element); } else { ConfigurationProperty property = new ConfigurationProperty(element.Name, type, null, ConfigurationPropertyOptions.None); this.properties.Add(property); } } } } }
string GetConfigurationElementName() { string configurationElementName = String.Empty; ExtensionElementCollection collection = null; Type extensionSectionType = ThisType; ContextInformation evaluationContext = this.ContainingEvaluationContext; if (evaluationContext == null) { evaluationContext = ConfigurationHelpers.GetEvaluationContext(this); } if (String.IsNullOrEmpty(this.extensionCollectionName)) { #if DESKTOP if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.ExtensionCollectionNameNotFound, SR.GetString(SR.TraceCodeExtensionCollectionNameNotFound), this, (Exception)null); } #endif collection = ExtensionsSection.UnsafeLookupAssociatedCollection(ThisType, evaluationContext, out this.extensionCollectionName); } else { collection = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, evaluationContext); } if (null == collection) { if (String.IsNullOrEmpty(this.extensionCollectionName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigNoExtensionCollectionAssociatedWithType, extensionSectionType.AssemblyQualifiedName), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionCollectionNotFound, this.extensionCollectionName), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } } for (int i = 0; i < collection.Count; i++) { ExtensionElement collectionElement = collection[i]; // Optimize for assembly qualified names. if (collectionElement.Type.Equals(extensionSectionType.AssemblyQualifiedName, StringComparison.Ordinal)) { configurationElementName = collectionElement.Name; break; } // Check type directly for the case that the extension is registered with something less than // an full assembly qualified name. Type collectionElementType = Type.GetType(collectionElement.Type, false); if (null != collectionElementType && extensionSectionType.Equals(collectionElementType)) { configurationElementName = collectionElement.Name; break; } } if (String.IsNullOrEmpty(configurationElementName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionTypeNotRegisteredInCollection, extensionSectionType.AssemblyQualifiedName, this.extensionCollectionName), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } return(configurationElementName); }