public EmbeddedConfigurationProvider(Stream fileStream)
        {
            var source = new TSource();

            _provider = source.Build(new ConfigurationBuilder()) as FileConfigurationProvider;
            _provider.Load(fileStream);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public override void Load(Stream stream)
        {
            _configurationProvider.Load(stream);

            // Получим список ключей
            var keys = _configurationProvider.GetKeys();

            bool IsIncludeKey(string key) => key.EndsWith("${include}");
            bool IsNotIncludeKey(string key) => !IsIncludeKey(key);

            // Ключи с ${include}
            var keysWithIncludes = keys.Where(IsIncludeKey).ToArray();

            if (keysWithIncludes.Length > 0)
            {
                foreach (var include in keysWithIncludes)
                {
                    if (_configurationProvider.TryGet(include, out string includePath))
                    {
                        var path     = Path.Combine(_rootPath, includePath);
                        var fullPath = Path.GetFullPath(path);

                        // Создадим провайдер конфигурации и загрузим значения из него
                        var jsonConfigurationProvider = CreateConfigurationProvider(fullPath);
                        jsonConfigurationProvider.Load();

                        // Получим все ключи
                        var keysToInclude = jsonConfigurationProvider.GetKeys();

                        // Добавим все данные из подгруженного файла
                        jsonConfigurationProvider.AddValuesToDictionary(keysToInclude, Data);
                    }
                }

                // Добавим ключи, которые были в базовом провайдере
                var otherKeys = keys.Where(IsNotIncludeKey).ToArray();
                _configurationProvider.AddValuesToDictionary(otherKeys, Data);
            }
            else
            {
                // Добавим ключи, которые были в базовом провайдере
                _configurationProvider.AddValuesToDictionary(keys, Data);
            }
        }
Esempio n. 3
0
        /// <summary>Attempts to bind the given object instance to configuration section value by FileConfigurationProvider.</summary>
        /// <param name="options">The object to bind.</param>
        /// <param name="value">The value to bind.</param>
        /// <param name="provider">The <see cref="T:FileConfigurationProvider" /> to load the value.</param>
        public static void Bind <TOptions>([NotNull] TOptions options, string value, [NotNull] FileConfigurationProvider provider) where TOptions : class
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrWhiteSpace(value))
            {
                return;
            }
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            var root = new ConfigurationRoot(new List <IConfigurationProvider> {
                provider
            });

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(value)))
                provider.Load(stream);

            root.Bind(options);
        }
Esempio n. 4
0
 /// <inheritdoc />
 public override void Load(Stream stream)
 {
     _configurationProvider.Load(stream);
     LoadRecursive(_configurationProvider, Data, new HashSet <string>());
 }