Ejemplo n.º 1
0
        public void CreateListener(ConfigurationLevels level, string documentPath, Func <ConfigurationLevels, string, Task> LoadOnChangeAsyncCallback)
        {
            var document = FirestoreClient.Document(documentPath);

            ConfigurationDocuments.Add(level, document);
            document.Listen(async snapshot => await LoadOnChangeAsyncCallback(level, snapshot.Id));
        }
        public async Task LoadDocumentSettingsOnChangeAsync(ConfigurationLevels level, string snapshotId)
        {
            _logger.LogInformation($"Configuration change detected... Level: {level}, Document: {snapshotId}");
            //Remove all keys for a new load.
            ConfigData.Clear();
            //When a change is made in one level we must load all other levels in order to merge all settings ordered by relevance (application -> stage -> machine).
            foreach (var configurationLevel in _connectionManager.GetConfigurationDocumentLevels())
            {
                _logger.LogDebug($"Loading Documents by Level. Current:{configurationLevel}");
                var remoteSettingsDocument = await _connectionManager.GetDocumentFieldsAsync(configurationLevel);

                if (remoteSettingsDocument.Count > 0)
                {
                    //Use this FirestoreConfigurationProvider method to convert the json settings into a dictionary with IConfiguration format.
                    var dataDictionary = JsonSettingsToDictionarySettings(remoteSettingsDocument.ToJson());
                    //Add settings to a centralized final dictionary.
                    dataDictionary.ToList().ForEach(item => ConfigData.AddOrUpdate(item.Key.ToLower(), item.Value, (key, value) => value = item.Value));
                    //Resolve configuration secrets if exists.
                    var secretValues = await _secretsManager.ResolveSecretsAsync(dataDictionary, _options.ProjectId);

                    secretValues.ForEach(item => ConfigData.AddOrUpdate(item.Key.ToLower(), item.Value, (key, value) => value = item.Value));
                }
            }
            //Use this FirestoreConfigurationProvider method in order to have access the private Data Dictionary and refresh the token.
            ReloadSettings(ConfigData);
            _logger.LogDebug($"End of detected change load! {DateTime.Now}");
        }
Ejemplo n.º 3
0
        public async Task RemoveListener(ConfigurationLevels level)
        {
            if (ConfigurationListeners.ContainsKey(level))
            {
                await ConfigurationListeners[level].StopAsync();
            }

            if (ConfigurationDocuments.ContainsKey(level))
            {
                ConfigurationDocuments.Remove(level);
            }
        }
        public async Task <Dictionary <string, object> > GetDocumentFieldsAsync(ConfigurationLevels level)
        {
            if (ConfigurationDocuments.TryGetValue(level, out var document))
            {
                var snapshot = await document.GetSnapshotAsync();

                if (snapshot.Exists)
                {
                    return(snapshot.ToDictionary());
                }
            }
            return(new Dictionary <string, object>());
        }