Ejemplo n.º 1
0
    public object GetSection(string configKey)
    {
        //Get original configuration section
        if (_originalConfig == null)
        {
            Trace.TraceWarning("ConfigurationProxy: _originalConfig is null");
            return(null);
        }
        object originalSection = _originalConfig.GetSection(configKey);

        if (TestConfig == null)
        {
            Trace.TraceWarning("ConfigurationProxy: TestConfig is null");
            return(originalSection);
        }

        //merge app settings
        if (configKey == "appSettings")
        {
            return(GetMergedAppSettings(originalSection));
        }

        //merge connectionstrings
        if (configKey == "connectionStrings")
        {
            return(GetMergedConnectionStringSection(originalSection));
        }

        //handle request for dataSources section
        if (configKey == "microsoft.visualstudio.testtools")
        {
            return(GetMergedDataSourcesSection(configKey, originalSection));
        }
        return(originalSection);
    }
Ejemplo n.º 2
0
        /// <summary>
        /// This is central processing logic.  To support additional sections, you extend the switch statement
        /// </summary>
        /// <param name="configSection">Name of first level configuration section</param>
        /// <returns>One of the strongly typed configuration classes</returns>
        public object GetSection(string configSection)
        {
            switch (configSection)
            {
            case "appSettings":
                loadAndMergeAppSettings();
                return(_appSettings);

            case "connectionStrings":
                loadAndMergeConnectionStrings();
                return(_nativeConfigSystem.GetSection(configSection));

            default:
                return(_nativeConfigSystem.GetSection(configSection));
            }
        }
        /// <summary>
        /// 依據指定的索引鍵傳回組態物件。
        /// </summary>
        /// <param name="configKey">組態索引鍵值。</param>
        /// <returns>組態物件。</returns>
        public object GetSection(string configKey)
        {
            // get the section from the default location (web.config or app.config)
            object section = clientConfigSystem.GetSection(configKey);

            return(section);
        }
        /// <summary>
        /// Retrieve config object based on key. If connectionstrings, we want to return our modified
        /// config object. Otherwise, return baseconf's implementation.
        /// </summary>
        /// <param name="configKey"></param>
        /// <returns></returns>
        public object GetSection(string configKey)
        {
            // if our modified ConnectionStringsSection object is set, return.
            if (configKey == "connectionStrings" && _connectionStrings != null)
            {
                return(_connectionStrings);
            }

            // get section from config file on disk
            object o = _baseconf.GetSection(configKey);

            // copy all original connection strings into ours
            if (configKey == "connectionStrings" && o is ConnectionStringsSection)
            {
                ConnectionStringSettingsCollection baseCsCollection = ((ConnectionStringsSection)o).ConnectionStrings;

                // create a new ConnectionStringsSection because we're unable to set base ConnectionStringSettingsCollection.
                ConnectionStringsSection newCsSection = new ConnectionStringsSection();
                // copy each ConnectionStringSettings from
                foreach (ConnectionStringSettings baseCss in baseCsCollection)
                {
                    var newCs = DecryptConnectionStringPassword(baseCss.ConnectionString);
                    ConnectionStringSettings newCss = new ConnectionStringSettings(baseCss.Name, newCs, baseCss.ProviderName);
                    newCsSection.ConnectionStrings.Add(newCss);
                }
                o = _connectionStrings = newCsSection;
            }
            return(o);
        }
 public static object GetSection(string sectionName)
 {
     if (string.IsNullOrEmpty(sectionName))
     {
         return(null);
     }
     PrepareConfigSystem();
     return(s_configSystem.GetSection(sectionName));
 }
        public static object GetSection(string sectionName)
        {
            object o = ConfigurationSystem.GetSection(sectionName);

            if (o is ConfigurationSection)
            {
                return(((ConfigurationSection)o).GetRuntimeObject());
            }
            else
            {
                return(o);
            }
        }
        public object GetSection(string configKey)
        {
            if (configKey == "appSettings" && this.appsettings != null)
            {
                return(this.appsettings);
            }
            object section = baseconf.GetSection(configKey);

            if (configKey == "appSettings" && section is NameValueCollection)
            {
                section = this.appsettings = KeyVaultProvider.AppSettings;
            }
            return(section);
        }
Ejemplo n.º 8
0
        // GetSection
        //
        // Retrieve a Section from the configuration system
        //
        public static object GetSection(string sectionName)
        {
            // Avoid unintended AV's by ensuring sectionName is not empty.
            // For compatibility, we cannot throw an InvalidArgumentException.
            if (String.IsNullOrEmpty(sectionName))
            {
                return(null);
            }

            PrepareConfigSystem();

            object section = s_configSystem.GetSection(sectionName);

            return(section);
        }
Ejemplo n.º 9
0
        public object GetSection(string sectionName)
        {
            if (sectionName != VisualStudioSetingsFileConnectionStringProviderTests.SETTING_SECTION)
            {
                return(oldConfigSystem?.GetSection(sectionName));
            }

            var settingElement = new SettingElement(VisualStudioSetingsFileConnectionStringProviderTests.SETTING_KEY, SettingsSerializeAs.String);
            var settingValue   = new SettingValueElement();

            settingValue.ValueXml           = new XmlDocument().CreateElement("value");
            settingValue.ValueXml.InnerText = VisualStudioSetingsFileConnectionStringProviderTests.SETTING_VALUE;
            settingElement.Value            = settingValue;
            var section = new ClientSettingsSection();

            section.Settings.Add(settingElement);

            return(section);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Get the specified section
        /// </summary>
        /// <param name="contextName"></param>
        /// <param name="parent"></param>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public object GetSection(string contextName, object parent, string sectionName)
        {
            EnsureInit();
            ConfigurationSection thisSection = _configuration.GetSection(sectionName);

            //object parent = null;
            if (_next != null)
            {
                parent = _next.GetSection(sectionName);
            }
            if (thisSection == null)
            {
                return(parent);
            }

            object result = resolveSectionRuntimeObject(thisSection);

            if (result is DefaultSection)
            {
                string rawXml = thisSection.SectionInformation.GetRawXml();
                if (string.IsNullOrEmpty(rawXml))
                {
                    return(null);
                }

                Type t = TypeResolutionUtils.ResolveType(thisSection.SectionInformation.Type);
                //ContextHandler ch = new ContextHandler();
                if (typeof(IConfigurationSectionHandler).IsAssignableFrom(t))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(thisSection.SectionInformation.GetRawXml());
                    IConfigurationSectionHandler handler = (IConfigurationSectionHandler)Activator.CreateInstance(t);
                    return(handler.Create(parent, null, SetContextName(contextName, xmlDoc.DocumentElement)));
                }
                throw new ConfigurationErrorsException(string.Format(" <section>配置节点没有声明'{0}'", sectionName));
            }
            return(result);
        }
Ejemplo n.º 11
0
        public object GetSection(string configKey)
        {
            // get the section from the default location (web.config or app.config)
            object section = clientConfigSystem.GetSection(configKey);

            switch (configKey)
            {
            case "appSettings":
                if (this.appsettings != null)
                {
                    return(this.appsettings);
                }

                if (section is NameValueCollection)
                {
                    // create a new collection because the underlying collection is read-only
                    var cfg = new NameValueCollection();
                    NameValueCollection localSettings = (NameValueCollection)section;
                    foreach (string key in localSettings)
                    {
                        cfg.Add(key, localSettings[key]);
                    }

                    // merge the settings from core with the local appsettings
                    this.appsettings = cfg.Merge(ConfigurationManager.AppSettings);
                    section          = this.appsettings;
                }

                break;

            case "connectionStrings":
                if (this.connectionStrings != null)
                {
                    return(this.connectionStrings);
                }

                // create a new collection because the underlying collection is read-only
                var cssc = new ConnectionStringSettingsCollection();

                // copy the existing connection strings into the new collection
                foreach (ConnectionStringSettings connectionStringSetting in ((ConnectionStringsSection)section).ConnectionStrings)
                {
                    cssc.Add(connectionStringSetting);
                }

                // merge the settings from core with the local connectionStrings
                cssc = cssc.Merge(ConfigurationManager.ConnectionStrings);

                ConnectionStringsSection connectionStringsSection = new ConnectionStringsSection();

                foreach (ConnectionStringSettings connectionStringSetting in cssc)
                {
                    connectionStringsSection.ConnectionStrings.Add(connectionStringSetting);
                }

                this.connectionStrings = connectionStringsSection;
                section = this.connectionStrings;
                break;
            }

            return(section);
        }
        /// <summary>
        /// Retrieve config object based on key. If connectionstrings, we want to return our modified
        /// config object. Otherwise, return baseconf's implementation.
        /// </summary>
        /// <param name="configKey"></param>
        /// <returns></returns>
        public object GetSection(string configKey)
        {
            if (configKey != "connectionStrings" && configKey != "appSettings")
            {
                return(_baseconf.GetSection(configKey));
            }

            // we'll keep a collection that includes both appSettings and SecureSettings so that they can
            //  both be accessed via appSettings
            if (configKey == "appSettings")
            {
                if (_appSettings == null)
                {
                    lock (lockme)
                    {
                        if (_appSettings == null)
                        {
                            NameValueCollection appSettingsSection = (NameValueCollection)_baseconf.GetSection(configKey);
                            // first add appSettings to the a new collection
                            NameValueCollection newAppSettings = new NameValueCollection(appSettingsSection);
                            // then add SecureSettings with decrypted values.
                            if (Settings.SecureSetting != null)
                            {
                                foreach (SecureSetting setting in Settings.SecureSetting)
                                {
                                    if (appSettingsSection[setting.Key] != null)
                                    {
                                        throw new ApplicationException(String.Format("There's already an appSetting with key: {0}.  Can't have SecureSetting with the same key.", setting.Key));
                                    }
                                    newAppSettings.Add(setting.Key, setting.Value);
                                }
                            }
                            _appSettings = newAppSettings;
                        }
                    }
                }
                return(_appSettings);
            }

            // the ConnectionStringsSection is being requested.  If we don't yet have it cached, cache it.
            if (_connectionStrings == null)
            {
                lock (lockme)
                {
                    if (_connectionStrings == null)
                    {
                        ConnectionStringsSection csSection = _baseconf.GetSection(configKey) as ConnectionStringsSection;
                        if (csSection == null) // not sure how this could happen, but just in case
                        {
                            throw new ApplicationException(String.Format("ConfigKey: {0} does not cast to ConnectionStringsSection?!", configKey));
                        }

                        ConnectionStringSettingsCollection baseCsCollection = csSection.ConnectionStrings;

                        // create a new ConnectionStringsSection because we're unable to set base ConnectionStringSettingsCollection.
                        ConnectionStringsSection newCsSection = new ConnectionStringsSection();
                        // copy each ConnectionStringSettings from
                        foreach (ConnectionStringSettings baseCss in baseCsCollection)
                        {
                            var newCs = DecryptConnectionStringPassword(baseCss.ConnectionString);
                            ConnectionStringSettings newCss = new ConnectionStringSettings(baseCss.Name, newCs, baseCss.ProviderName);
                            newCsSection.ConnectionStrings.Add(newCss);
                        }
                        _connectionStrings = newCsSection;
                    }
                }
            }
            return(_connectionStrings);
        }
Ejemplo n.º 13
0
        public object GetSection(string configKey)
        {
            object section = clientConfigSystem.GetSection(configKey);

            switch (configKey)
            {
            case "appSettings":
                if (appSettings != null)
                {
                    return(appSettings);
                }
                if (section is NameValueCollection)
                {
                    if (configProviders != null)
                    {
                        var cfg = new NameValueCollection();
                        NameValueCollection localSettings = (NameValueCollection)section;
                        foreach (string key in localSettings)
                        {
                            cfg.Add(key, localSettings[key]);
                        }
                        foreach (var configProvider in configProviders)
                        {
                            var settings = configProvider.GetAppSettings();
                            if (settings != null)
                            {
                                foreach (string key in settings)
                                {
                                    cfg.Add(key, settings[key]);
                                }
                            }
                        }
                        this.appSettings = cfg;
                        section          = this.appSettings;
                    }
                    else
                    {
                        this.appSettings = section;
                    }
                }
                break;

            case "connectionStrings":
                if (this.connectionStrings != null)
                {
                    return(this.connectionStrings);
                }

                if (configProviders != null)
                {
                    ConnectionStringsSection connectionStringsSection = new ConnectionStringsSection();
                    foreach (ConnectionStringSettings connectionStringSetting in ((ConnectionStringsSection)section).ConnectionStrings)
                    {
                        connectionStringsSection.ConnectionStrings.Add(connectionStringSetting);
                    }
                    foreach (var configProvider in configProviders)
                    {
                        var conns = configProvider.GetConnectionStrings();
                        if (conns != null)
                        {
                            foreach (ConnectionStringSettings connectionStringSetting in conns)
                            {
                                connectionStringsSection.ConnectionStrings.Add(connectionStringSetting);
                            }
                        }
                    }
                    this.connectionStrings = connectionStringsSection;
                    section = this.connectionStrings;
                }
                else
                {
                    this.connectionStrings = section;
                }
                break;
            }

            return(section);
        }
Ejemplo n.º 14
0
        public object GetSection(string configKey)
        {
            // get the section from the default location (web.config or app.config)
            object section = clientConfigSystem.GetSection(configKey);

            switch (configKey)
            {
            case "appSettings":
                // Return cached version if exists
                if (this.appsettings != null)
                {
                    return(this.appsettings);
                }
                // create a new collection because the underlying collection is read-only
                var cfg = new NameValueCollection();
                // If an AppSettings section exists in Web.config, read and add values from it
                if (section is NameValueCollection)
                {
                    NameValueCollection localSettings = (NameValueCollection)section;
                    foreach (string key in localSettings)
                    {
                        cfg.Add(key, localSettings[key]);
                    }
                }
                // --------------------------------------------------------------------
                // Here I read and decrypt keys and add them to secureConfig dictionary
                // To test assume the following line is a key stored in secure sotrage.
                //secureConfig = SecureConfig.LoadConfig();
                secureConfig.Add("ACriticalKey", "VeryCriticalValue");
                // --------------------------------------------------------------------
                foreach (KeyValuePair <string, string> item in secureConfig)
                {
                    if (cfg.AllKeys.Contains(item.Key))
                    {
                        cfg[item.Key] = item.Value;
                    }
                    else
                    {
                        cfg.Add(item.Key, item.Value);
                    }
                }
                // --------------------------------------------------------------------
                // Cach the settings for future use
                this.appsettings = cfg;
                // return the merged version of the items from secure storage and appsettings
                section = this.appsettings;
                break;

            case "connectionStrings":
                // Return cached version if exists
                if (this.connectionStrings != null)
                {
                    return(this.connectionStrings);
                }
                // create a new collection because the underlying collection is read-only
                ConnectionStringsSection connectionStringsSection = new ConnectionStringsSection();
                // copy the existing connection strings into the new collection
                foreach (ConnectionStringSettings connectionStringSetting in ((ConnectionStringsSection)section).ConnectionStrings)
                {
                    connectionStringsSection.ConnectionStrings.Add(connectionStringSetting);
                }
                // --------------------------------------------------------------------
                // Again Load connection strings from secure storage and merge like below
                // connectionStringsSection.ConnectionStrings.Add(connectionStringSetting);
                // --------------------------------------------------------------------
                // Cach the settings for future use
                this.connectionStrings = connectionStringsSection;
                // return the merged version of the items from secure storage and appsettings
                section = this.connectionStrings;
                break;
            }
            return(section);
        }
 protected override object GetAppWebSection(string sectionName)
 {
     return(originalConfiguration.GetSection(sectionName));
 }