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); } } } } } }
internal static ExtensionElementCollection UnsafeLookupCollection(string collectionName, ContextInformation evaluationContext) { ExtensionsSection section = null; if (evaluationContext != null) { section = (ExtensionsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.ExtensionsSectionPath); } else { if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, 0x80018, System.ServiceModel.SR.GetString("TraceCodeEvaluationContextNotFound"), null, (Exception)null); } section = (ExtensionsSection)ConfigurationHelpers.UnsafeGetSection(ConfigurationStrings.ExtensionsSectionPath); } switch (collectionName) { case "behaviorExtensions": return(section.BehaviorExtensions); case "bindingElementExtensions": return(section.BindingElementExtensions); case "bindingExtensions": return(section.BindingExtensions); case "endpointExtensions": return(section.EndpointExtensions); } DiagnosticUtility.FailFast(string.Format(CultureInfo.InvariantCulture, "{0} is not a valid ServiceModelExtensionsSection collection name.", new object[] { collectionName })); return(null); }
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); }
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); }
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); }
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); } } } } }
internal static ExtensionElementCollection UnsafeLookupAssociatedCollection(Type extensionType, ContextInformation evaluationContext, out string collectionName) { collectionName = GetExtensionType(extensionType); return(ExtensionsSection.UnsafeLookupCollection(collectionName, evaluationContext)); }
/// <summary> /// This method adds the neccessary configuration elements to hook up /// Thinktecture.ServiceModel.Extensions.Metdata extension to service the /// service code being generated. /// </summary> private void AddMetadataServiceBehavior() { // Get a pointer to system.serviceModel section. ConfigurationSectionGroup csg = configuration.SectionGroups["system.serviceModel"]; // Notify if we get a null reference. Debug.Assert(csg != null, "system.serviceModel section could not be found in the configuration."); if (csg != null) { // Try to find the extensions element. ExtensionsSection extensionsSection = csg.Sections["extensions"] as ExtensionsSection; // Create it if it wasn't found. if (extensionsSection == null) { extensionsSection = new ExtensionsSection(); csg.Sections.Add("extensions", extensionsSection); } // Now create the new behavior extension. ExtensionElement metadataServiceExtensionElement = new ExtensionElement(); metadataServiceExtensionElement.Name = "metadataService"; //TODO: Make this more dynamic so it can discover the assembly version etc otherwise this will always throw exceptions // that the behavior extension was not found in the collection. metadataServiceExtensionElement.Type = "Thinktecture.ServiceModel.Extensions.Metadata.StaticMetadataBehaviorElement, Thinktecture.ServiceModel.Extensions.Metadata, Version=1.0.14.0, Culture=neutral, PublicKeyToken=20fb7cabbfb92df4"; // Add the newly created behavior extension to the extensions section. extensionsSection.BehaviorExtensions.Add(metadataServiceExtensionElement); // Try to find the behaviors element. BehaviorsSection behaviorsSection = csg.Sections["behaviors"] as BehaviorsSection; // Create it if it wasn't found. if (behaviorsSection == null) { behaviorsSection = new BehaviorsSection(); csg.Sections.Add("behaviors", behaviorsSection); } // Add the new service behavior. ServiceBehaviorElement serviceBehavior = new ServiceBehaviorElement(); serviceBehavior.Name = "metadataServiceExtension"; behaviorsSection.ServiceBehaviors.Add(serviceBehavior); StaticMetadataBehaviorElement behaviorExtensionElement = new StaticMetadataBehaviorElement(); behaviorExtensionElement.RootMetadataFileLocation = options.MetadataLocation; behaviorExtensionElement.MetadataUrl = "wsdl"; serviceBehavior.Add(behaviorExtensionElement); // Find the service section. ServicesSection servicesSection = csg.Sections["services"] as ServicesSection; if (servicesSection != null) { string fqServiceTypeName = GetFullyQulifiedTypeName(GetServiceTypeName()); ServiceElement serviceElement = servicesSection.Services[fqServiceTypeName] as ServiceElement; if (serviceElement != null) { serviceElement.BehaviorConfiguration = "metadataServiceExtension"; } } } }
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); }