Esempio n. 1
0
        public async Task <(ConfigItemsCollection Configs, DateTime?LastModified)> GetConfiguration()
        {
            var      conf = new Dictionary <string, ConfigItem>(StringComparer.OrdinalIgnoreCase);
            DateTime?latestConfigFileModifyTime = null;

            foreach (var configFile in _configurationLocations.ConfigFileDeclarations.SelectMany(FindConfigFiles))
            {
                var modifyTime = await ReadConfiguration(configFile, conf).ConfigureAwait(false);

                if (latestConfigFileModifyTime == null || modifyTime > latestConfigFileModifyTime)
                {
                    latestConfigFileModifyTime = modifyTime;
                }
            }

            var collection = new ConfigItemsCollection(conf.Values);

            var notFoundEnvVariables = new List <string>();

            foreach (var configItem in collection.Items)
            {
                var list = paramMatcher.Matches(configItem.RawValue)
                           .Cast <Match>()
                           .Select(match => new
                {
                    Placehodler = "%" + match.Groups[1].Value + "%",
                    Value       = _environment[match.Groups[1].Value.ToUpperInvariant()]
                }).ToList();

                if (list.Any())
                {
                    var notFound = list.Where(a => string.IsNullOrEmpty(a.Value)).Select(a => a.Placehodler.Trim('%')).ToArray();

                    if (!notFound.Any())
                    {
                        foreach (var valToReplace in list)
                        {
                            configItem.Value = configItem.Value.Replace(valToReplace.Placehodler, valToReplace.Value);
                        }
                    }
                    else
                    {
                        notFoundEnvVariables.AddRange(notFound);
                    }
                }
            }

            if (notFoundEnvVariables.Any())
            {
                throw new EnvironmentException("Configuration is dependent on following enviroment variables:" + string.Join("\n", notFoundEnvVariables) + "\n but they are not set.");
            }

            // return merged configuration
            return(collection, latestConfigFileModifyTime);
        }
Esempio n. 2
0
        public async Task <ConfigItemsCollection> GetConfiguration()
        {
            var conf = new Dictionary <string, ConfigItem>();

            foreach (var configFile in _configurationLocations.ConfigFileDeclarations.SelectMany(FindConfigFiles))
            {
                await ReadConfiguration(configFile, conf).ConfigureAwait(false);
            }

            var collection = new ConfigItemsCollection(conf.Values);

            var notFoundEnvVariables = new List <string>();

            foreach (var configItem in collection.Items)
            {
                var list = paramMatcher.Matches(configItem.RawValue)
                           .Cast <Match>()
                           .Select(match => new
                {
                    Placehodler = "%" + match.Groups[1].Value + "%",
                    Value       = _environmentVariableProvider.GetEnvironmentVariable(match.Groups[1].Value)
                }).ToList();

                if (list.Any())
                {
                    var notFound = list.Where(a => string.IsNullOrEmpty(a.Value)).Select(a => a.Placehodler.Trim('%')).ToArray();

                    if (!notFound.Any())
                    {
                        foreach (var valToReplace in list)
                        {
                            configItem.Value = configItem.Value.Replace(valToReplace.Placehodler, valToReplace.Value);
                        }
                    }
                    else
                    {
                        notFoundEnvVariables.AddRange(notFound);
                    }
                }
            }

            if (notFoundEnvVariables.Any())
            {
                throw new EnvironmentException("Configuration is dependent on following enviroment variables:" + string.Join("\n", notFoundEnvVariables) + "\n but they are not set.");
            }

            // return merged configuration
            return(collection);
        }