Esempio n. 1
0
        /// <summary>
        /// Gets the connection name from environment.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="unqualifiedKey">The unqualified key.</param>
        /// <param name="environmentName">Name of the environment.</param>
        /// <param name="delimiter">The delimiter.</param>
        /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">unqualifiedKey - The expected App Settings key is not here.</exception>
        /// <exception cref="ConfigurationErrorsException"></exception>
        public static string GetConnectionNameFromEnvironment(this ConnectionStringSettingsCollection collection, string unqualifiedKey, string environmentName, string delimiter, bool throwConfigurationErrorsException)
        {
            if (collection == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(unqualifiedKey))
            {
                throw new ArgumentNullException("unqualifiedKey", "The expected App Settings key is not here.");
            }

            var name1 = string.Concat(environmentName, delimiter, unqualifiedKey);
            var name2 = string.Concat(unqualifiedKey, delimiter, environmentName);

            var containsKey1 = collection.OfType <ConnectionStringSettings>().Any(i => i.Name == name1);

            if (!containsKey1 && !collection.OfType <ConnectionStringSettings>().Any(i => i.Name == name2))
            {
                if (throwConfigurationErrorsException)
                {
                    throw new ConfigurationErrorsException(string.Format("The expected Name, “{0}” or “{1}”, is not here.", name1, name2));
                }
                return(null);
            }

            return(containsKey1 ? name1 : name2);
        }
Esempio n. 2
0
        /// <summary>
        /// Returns <see cref="ConnectionStringSettingsCollection" />
        /// with the application settings
        /// of the specified external configuration <see cref="XDocument" />.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="externalConfigurationDoc">The external configuration document.</param>
        /// <returns></returns>
        public static ConnectionStringSettingsCollection WithConnectionStringSettingsCollection(this ConnectionStringSettingsCollection collection, XDocument externalConfigurationDoc)
        {
            var externalCollection = externalConfigurationDoc.ToConnectionStringSettingsCollection();

            if (externalCollection == null)
            {
                return(null);
            }

            collection.OfType <ConnectionStringSettings>().ForEachInEnumerable(i => externalCollection.Add(i));

            return(externalCollection);
        }
Esempio n. 3
0
        public ConfigurationSource(IConfigurationManager configManager, Func <System.Configuration.Configuration, IConfiguration> configFactory)
        {
            _configManager = configManager;
            _configFactory = configFactory;
            NameValueCollection appSettings = _configManager.AppSettings;

            if (appSettings != null)
            {
                AppSettings = appSettings.AllKeys.ToDictionary(key => key, key => appSettings[key]);
            }
            ConnectionStringSettingsCollection connectionStrings = _configManager.ConnectionStrings;

            if (connectionStrings != null)
            {
                ConnectionStrings = connectionStrings.OfType <ConnectionStringSettings>().ToDictionary(settings => settings.Name, settings => settings);
            }
        }