コード例 #1
0
        public ConfigurationSection GetSection(string sectionName)
        {
            var sections = new List <ConfigurationSection>();

            // Read section from custom configuration files.
            ConfigurationSection emptySection = null;
            ConfigurationSection section;

            foreach (string fileName in FileNames) // The order of files should be from most Important to lower
            {
                section = GetFileSection(fileName, sectionName);

                // Filter out non-present in file sections (IsPresent == false).
                if (section != null)
                {
                    if (IsSectionPresentInConfigFile(section))
                    {
                        sections.Add(section);
                    }

                    // Do not add non present DefaultSections, they will be bank - so no handler processing occurred.
                    // Also this could prevent from returning Default system section. (as with section "system.web/browserCaps" under web)
                    else if (!(section is DefaultSection) && section.SectionInformation.IsDeclared)
                    {
                        emptySection = section; // declared but not present section - should be used if nothing else found.
                    }
                }
            }

            // Read Section form Configuration Manager.
            section = GetDefaultSection(sectionName);
            if (section != null) // Add not presented sections too, because of non required sections should be returned.
            {
                sections.Add(section);
            }


            if (emptySection != null)
            {
                sections.Add(emptySection);
            }

            // Merge collected sections.
            if (sections.Count > 1)
            {
                NSectionMerger merger = MergerRegistry.GetMerger(sections[0].GetType());
                return(merger.Merge(sections));
            }

            if (sections.Count == 1)
            {
                return(sections[0]);
            }

            return(null);
        }
コード例 #2
0
        public ConfigurationSection GetSection(string sectionName)
        {
            var sections = new List <ConfigurationSection>();

            // Read section from custom configuration files.
            ConfigurationSection emptySection = null;
            ConfigurationSection section;

            foreach (string fileName in FileNames) // The order of files should be from most Important to lower
            {
                section = GetFileSection(fileName, sectionName);

                // Filter out non-required sections (IsPresent == false) but leave DefaultSections, since them could represent IConfigurationSectionHandler sections.
                if (section != null)
                {
                    if (section.ElementInformation.IsPresent || section is DefaultSection)
                    {
                        sections.Add(section);
                    }
                    else
                    {
                        emptySection = section; // declared but not present section - should be used if nothing else found.
                    }
                }
            }

            // Read Section form Configuration Manager.
            section = GetDefaultSection(sectionName);
            if (section != null) // Add not presented sections too, because of non required sections should be returned.
            {
                sections.Add(section);
            }


            if (emptySection != null)
            {
                sections.Add(emptySection);
            }

            // Merge collected sections.
            if (sections.Count > 1)
            {
                NSectionMerger merger = MergerRegistry.GetMerger(sections[0].GetType());
                return(merger.Merge(sections));
            }

            if (sections.Count == 1)
            {
                return(sections[0]);
            }

            return(null);
        }
コード例 #3
0
        public ConfigurationSection GetSection(string sectionName)
        {
            var sections = new List <ConfigurationSection>();

            /*
             * Azure overrides appsettings/connectionstrings which are configured in the portal on first load of the web.config
             * But if we re-configure those settings in a {hostname}.custom.config then the ordering is incorrect.
             *
             * To fix this we can extract those portal-configured settings from the environment vars by their
             * prefix and add these to the initial section (which has the highest priority when merging config sections
             * back to one section
             */

            if (sectionName.Equals("AppSettings", StringComparison.OrdinalIgnoreCase))
            {
                var settings = GetAzureEnvironmentVariables(@"APPSETTING_");
                var environmentVarSection = new AppSettingsSection();
                foreach (var kv in settings)
                {
                    environmentVarSection.Settings.Add(kv.Key, kv.Value);
                }
                sections.Add(environmentVarSection);
            }
            else if (sectionName.Equals("ConnectionStrings", StringComparison.OrdinalIgnoreCase))
            {
                var connections = connectionStringTypeProviderNames
                                  .SelectMany(typeKeyVal => GetAzureEnvironmentVariables(typeKeyVal.Key)
                                              .Select(envVar => new ConnectionStringSettings(envVar.Key, envVar.Value, typeKeyVal.Value)));

                var environmentVarSection = new ConnectionStringsSection();
                foreach (var connection in connections)
                {
                    environmentVarSection.ConnectionStrings.Add(connection);
                }
                sections.Add(environmentVarSection);
            }

            // Read section from custom configuration files.
            ConfigurationSection emptySection = null;
            ConfigurationSection section;

            foreach (string fileName in FileNames) // The order of files should be from most Important to lower
            {
                section = GetFileSection(fileName, sectionName);

                // Filter out non-present in file sections (IsPresent == false).
                if (section != null)
                {
                    if (IsSectionPresentInConfigFile(section))
                    {
                        sections.Add(section);
                    }

                    // Do not add non present DefaultSections, they will be bank - so no handler processing occurred.
                    // Also this could prevent from returning Default system section. (as with section "system.web/browserCaps" under web)
                    else if (!(section is DefaultSection) && section.SectionInformation.IsDeclared)
                    {
                        emptySection = section; // declared but not present section - should be used if nothing else found.
                    }
                }
            }

            // Read Section form Configuration Manager.
            section = GetDefaultSection(sectionName);
            if (section != null) // Add not presented sections too, because of non required sections should be returned.
            {
                sections.Add(section);
            }


            if (emptySection != null)
            {
                sections.Add(emptySection);
            }

            // Merge collected sections.
            if (sections.Count > 1)
            {
                NSectionMerger merger = MergerRegistry.GetMerger(sections[0].GetType());
                return(merger.Merge(sections));
            }

            if (sections.Count == 1)
            {
                return(sections[0]);
            }

            return(null);
        }