internal static BindingCollectionElement UnsafeGetAssociatedBindingCollectionElement(ContextInformation evaluationContext, string bindingCollectionName) { BindingCollectionElement retVal = null; BindingsSection bindingsSection = (BindingsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.BindingsSectionGroupPath); if (null != bindingsSection) { bindingsSection.UpdateBindingSections(evaluationContext); try { retVal = bindingsSection[bindingCollectionName]; } catch (KeyNotFoundException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound, ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName)))); } catch (NullReferenceException) // System.Configuration.ConfigurationElement bug { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigBindingExtensionNotFound, ConfigurationHelpers.GetBindingsSectionPath(bindingCollectionName)))); } } return retVal; }
/// Be sure to update UnsafeGetAssociatedEndpointCollectionElement if you modify this method internal static EndpointCollectionElement GetAssociatedEndpointCollectionElement(ContextInformation evaluationContext, string endpointCollectionName) { EndpointCollectionElement retVal = null; StandardEndpointsSection endpointsSection = (StandardEndpointsSection)ConfigurationHelpers.GetAssociatedSection(evaluationContext, ConfigurationStrings.StandardEndpointsSectionPath); if (null != endpointsSection) { endpointsSection.UpdateEndpointSections(evaluationContext); try { retVal = (EndpointCollectionElement)endpointsSection[endpointCollectionName]; } catch (KeyNotFoundException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigEndpointExtensionNotFound, ConfigurationHelpers.GetEndpointsSectionPath(endpointCollectionName)))); } catch (NullReferenceException) // System.Configuration.ConfigurationElement bug { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigEndpointExtensionNotFound, ConfigurationHelpers.GetEndpointsSectionPath(endpointCollectionName)))); } } return retVal; }
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 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; }
/// <summary> /// Registers WCF services type mappings from the given configuration excluding specified endpoints if needed. /// </summary> public static IServiceCollection AddWcfServices(this IServiceCollection container, ContextInformation ctx, Func<ServiceEndpointElement, bool> exclEndpoints) { String servicesPath = "system.serviceModel/services"; ServicesSection services = (ServicesSection)(ctx != null ? ctx.GetSection(servicesPath) : ConfigurationManager.GetSection(servicesPath)); foreach (ServiceElement service in services.Services) { Type serviceType = GetType(service.Name); if (serviceType == null) continue; foreach (ServiceEndpointElement endpoint in service.Endpoints) { Type contractType = GetType(endpoint.Contract); if (contractType == null || endpoint.IsSystemEndpoint || exclEndpoints != null && exclEndpoints(endpoint)) continue; container.AddScoped(contractType, serviceType); } } return container; }
/// <summary> /// Registers WCF client type mappings from the given configuration excluding specified endpoints if needed. /// </summary> public static IServiceCollection AddWcfClientServices(this IServiceCollection container, ContextInformation ctx, Func<ChannelEndpointElement, bool> exclEndpoints) { String clientPath = "system.serviceModel/client"; ClientSection client = (ClientSection)(ctx != null ? ctx.GetSection(clientPath) : ConfigurationManager.GetSection(clientPath)); foreach (ChannelEndpointElement endpoint in client.Endpoints) { if (endpoint.Name == null || endpoint.Contract == null || exclEndpoints != null && exclEndpoints(endpoint)) continue; Type contractType = GetType(endpoint.Contract); Type factoryType = ctx != null ? typeof(ConfigurationChannelFactory<>).MakeGenericType(contractType) : typeof(ChannelFactory<>).MakeGenericType(contractType); container.AddSingleton(factoryType, sp => ctx == null ? Activator.CreateInstance(factoryType, endpoint.Name) : Activator.CreateInstance(factoryType, endpoint.Name, ctx, null)); container.AddScoped(contractType, sp => factoryType.InvokeMember("CreateChannel", BindingFlags.InvokeMethod, null, sp.GetService(factoryType), new object[] { })); } return container; }
internal static object GetAssociatedSection(ContextInformation evalContext, string sectionPath) { object section = null; if (evalContext != null) { section = evalContext.GetSection(sectionPath); } else { section = AspNetEnvironment.Current.GetConfigurationSection(sectionPath); if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.TraceEvent(TraceEventType.Verbose, 0x80024, System.ServiceModel.SR.GetString("TraceCodeGetConfigurationSection"), new StringTraceRecord("ConfigurationSection", sectionPath), null, null); } } if (section == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigSectionNotFound", new object[] { sectionPath }))); } return section; }
internal static BindingCollectionElement GetAssociatedBindingCollectionElement(ContextInformation evaluationContext, string bindingCollectionName) { BindingCollectionElement element = null; BindingsSection associatedSection = (BindingsSection) GetAssociatedSection(evaluationContext, ConfigurationStrings.BindingsSectionGroupPath); if (associatedSection != null) { associatedSection.UpdateBindingSections(evaluationContext); try { element = associatedSection[bindingCollectionName]; } catch (KeyNotFoundException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigBindingExtensionNotFound", new object[] { GetBindingsSectionPath(bindingCollectionName) }))); } catch (NullReferenceException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigBindingExtensionNotFound", new object[] { GetBindingsSectionPath(bindingCollectionName) }))); } } return element; }
internal static EndpointCollectionElement GetAssociatedEndpointCollectionElement(ContextInformation evaluationContext, string endpointCollectionName) { EndpointCollectionElement element = null; StandardEndpointsSection associatedSection = (StandardEndpointsSection) GetAssociatedSection(evaluationContext, ConfigurationStrings.StandardEndpointsSectionPath); if (associatedSection != null) { associatedSection.UpdateEndpointSections(evaluationContext); try { element = associatedSection[endpointCollectionName]; } catch (KeyNotFoundException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigEndpointExtensionNotFound", new object[] { GetEndpointsSectionPath(endpointCollectionName) }))); } catch (NullReferenceException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigEndpointExtensionNotFound", new object[] { GetEndpointsSectionPath(endpointCollectionName) }))); } } return element; }
internal static void ValidateServiceBehaviorReference(string behaviorConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement) { // ValidateBehaviorReference built on assumption that evaluationContext 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. if (null == evaluationContext) { Fx.Assert("ValidateBehaviorReference() should only called with valid ContextInformation"); DiagnosticUtility.FailFast("ValidateBehaviorReference() should only called with valid ContextInformation"); } if (!String.IsNullOrEmpty(behaviorConfiguration)) { BehaviorsSection behaviors = (BehaviorsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.BehaviorsSectionPath); if (!behaviors.ServiceBehaviors.ContainsKey(behaviorConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidServiceBehavior, behaviorConfiguration), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber)); } } }
internal static ExtensionElementCollection UnsafeLookupAssociatedCollection(Type extensionType, ContextInformation evaluationContext, out string collectionName) { collectionName = GetExtensionType(extensionType); return ExtensionsSection.UnsafeLookupCollection(collectionName, evaluationContext); }
internal static object UnsafeGetAssociatedSection(ContextInformation evalContext, string sectionPath) { object retval = null; if (evalContext != null) { retval = UnsafeGetSectionFromContext(evalContext, sectionPath); } else { retval = AspNetEnvironment.Current.UnsafeGetConfigurationSection(sectionPath); // Trace after call to underlying configuration system to // insure that configuration system is initialized if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.GetConfigurationSection, SR.GetString(SR.TraceCodeGetConfigurationSection), new StringTraceRecord("ConfigurationSection", sectionPath), null, null); } } if (retval == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ConfigurationErrorsException(SR.GetString(SR.ConfigSectionNotFound, sectionPath))); } 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 static void ValidateBindingReference(string binding, string bindingConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement) { if (evaluationContext == null) { DiagnosticUtility.FailFast("ValidateBindingReference() should only called with valid ContextInformation"); } if (!string.IsNullOrEmpty(binding)) { BindingCollectionElement element = null; if (evaluationContext != null) { element = ConfigurationHelpers.UnsafeGetAssociatedBindingCollectionElement(evaluationContext, binding); } else { element = ConfigurationHelpers.UnsafeGetBindingCollectionElement(binding); } if (element == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidSection", new object[] { ConfigurationHelpers.GetBindingsSectionPath(binding) }), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber)); } if (!string.IsNullOrEmpty(bindingConfiguration) && !element.ContainsKey(bindingConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidBindingName", new object[] { bindingConfiguration, ConfigurationHelpers.GetBindingsSectionPath(binding), "bindingConfiguration" }), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber)); } } }
internal void UpdateBindingSections(ContextInformation evaluationContext) { ExtensionElementCollection elements = ExtensionsSection.UnsafeLookupCollection("bindingExtensions", evaluationContext); if (elements.Count != this.properties.Count) { foreach (ExtensionElement element in elements) { if ((element != null) && !this.properties.Contains(element.Name)) { System.Type type = System.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 CommonBehaviorsSection UnsafeGetAssociatedSection(ContextInformation contextEval) { return (CommonBehaviorsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(contextEval, ConfigurationStrings.CommonBehaviorsSectionPath); }
internal static BehaviorsSection UnsafeGetAssociatedSection(ContextInformation evalContext) { return (BehaviorsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evalContext, ConfigurationStrings.BehaviorsSectionPath); }
internal static void ValidateEndpointReference(string endpoint, string endpointConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement) { // ValidateEndpointReference built on assumption that evaluationContext 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. if (null == evaluationContext) { Fx.Assert("ValidateEndpointReference() should only called with valid ContextInformation"); DiagnosticUtility.FailFast("ValidateEndpointReference() should only called with valid ContextInformation"); } if (!String.IsNullOrEmpty(endpoint)) { EndpointCollectionElement endpointCollectionElement = null; if (null != evaluationContext) { endpointCollectionElement = ConfigurationHelpers.UnsafeGetAssociatedEndpointCollectionElement(evaluationContext, endpoint); } else { endpointCollectionElement = ConfigurationHelpers.UnsafeGetEndpointCollectionElement(endpoint); } if (endpointCollectionElement == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidSection, ConfigurationHelpers.GetEndpointsSectionPath(endpoint)), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber)); } if (!String.IsNullOrEmpty(endpointConfiguration)) { if (!endpointCollectionElement.ContainsKey(endpointConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidEndpointName, endpointConfiguration, ConfigurationHelpers.GetEndpointsSectionPath(endpoint), ConfigurationStrings.EndpointConfiguration), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber)); } } } }
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; }
internal static object UnsafeGetSectionFromContext(ContextInformation evalContext, string sectionPath) { return evalContext.GetSection(sectionPath); }
internal void OnReset(ConfigurationElement parent) { this.reset = true; this.inheritedContext = ConfigurationHelpers.GetOriginalEvaluationContext(parent as IConfigurationContextProviderInternal); }
internal static ClientSection UnsafeGetSection(ContextInformation contextInformation) { return (ClientSection) ConfigurationHelpers.UnsafeGetSectionFromContext(contextInformation, ConfigurationStrings.ClientSectionPath); }
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); } } } } } }