public IConfigurationRoot Build()
        {
            foreach (var configuration in _remoteConfigurationOptions.Configurations)
            {
                var source = new RemoteConfigurationSource
                {
                    ConfigurationServiceUri = _remoteConfigurationOptions.ServiceUri,
                    HttpMessageHandler      = _remoteConfigurationOptions.HttpMessageHandler,
                    RequestTimeout          = _remoteConfigurationOptions.RequestTimeout,
                    LoggerFactory           = _remoteConfigurationOptions.LoggerFactory,

                    ConfigurationName = configuration.ConfigurationName,
                    Optional          = configuration.Optional,
                    ReloadOnChange    = configuration.ReloadOnChange,
                    Parser            = configuration.Parser,

                    CreateSubscriber = _remoteConfigurationOptions.CreateSubscriber
                };

                Add(source);
            }

            return(_configurationBuilder.Build());
        }
示例#2
0
        public RemoteConfigurationProvider(RemoteConfigurationSource source)
        {
            _source = source ?? throw new ArgumentNullException(nameof(source));

            if (source.ConfigurationName == null)
            {
                throw new ArgumentNullException(nameof(source.ConfigurationName));
            }

            if (source.ConfigurationServiceUri == null)
            {
                throw new ArgumentNullException(nameof(source.ConfigurationServiceUri));
            }

            Logger.LoggerFactory = source.LoggerFactory ?? new NullLoggerFactory();

            _logger = Logger.CreateLogger <RemoteConfigurationProvider>();

            _logger.LogInformation("Initializing remote configuration source for configuration '{ConfigurationName}'.", source.ConfigurationName);

            _httpClient = new Lazy <HttpClient>(CreateHttpClient);

            _parser = source.Parser;

            if (_parser == null)
            {
                var extension = Path.GetExtension(source.ConfigurationName).ToLower();

                _logger.LogInformation("A file parser was not specified. Attempting to resolve parser from file extension '{extension}'.", extension);

                switch (extension)
                {
                case ".ini":
                    _parser = new IniConfigurationFileParser();
                    break;

                case ".xml":
                    _parser = new XmlConfigurationFileParser();
                    break;

                case ".yaml":
                    _parser = new YamlConfigurationFileParser();
                    break;

                default:
                    _parser = new JsonConfigurationFileParser();
                    break;
                }
            }

            _logger.LogInformation("Using parser {Name}.", _parser.GetType().Name);

            if (source.ReloadOnChange)
            {
                if (source.Subscriber == null)
                {
                    _logger.LogWarning("ReloadOnChange is enabled but a subscriber has not been configured.");
                    return;
                }

                var subscriber = source.Subscriber();

                _logger.LogInformation("Initializing remote configuration {Name} subscriber for configuration '{ConfigurationName}'.", subscriber.Name, source.ConfigurationName);

                subscriber.Subscribe(source.ConfigurationName, message =>
                {
                    _logger.LogInformation("Received remote configuration change subscription for configuration '{ConfigurationName}' with hash {message}. " +
                                           "Current hash is {Hash}.", source.ConfigurationName, message, Hash);

                    if (message != null && message.ToString().Equals(Hash, StringComparison.OrdinalIgnoreCase))
                    {
                        _logger.LogInformation("Configuration '{ConfigurationName}' current hash {Hash} matches new hash. " +
                                               "Configuration will not be updated.", source.ConfigurationName, Hash);

                        return;
                    }

                    Load();
                    OnReload();
                });
            }
        }