示例#1
0
        public async Task Get()
        {
            try
            {
                var islocalcache = false;
                var apiurl       = _httpUrlRepository.GetApiUrl();
                var client       = _httpClientFactory.CreateClient();
                var response     = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, apiurl));

                response.EnsureSuccessStatusCode();
                var content = await response.Content.ReadAsStringAsync();

                var output = _jsonHelper.DeserializeObject <ApiInfo>(content);
                if (output != null && output.Value != null && output.Value.Count > 0)
                {
                    _dataList.WriteFullFence(output.Value);
                    islocalcache = true;
                }
                if (islocalcache)
                {
                    _localDataRepository.Set(_dataList.ReadFullFence());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"加载配置错误");
                _dataList.WriteFullFence(_localDataRepository.Get());
            }
        }
        public async Task Get(bool reload)
        {
            try
            {
                if (reload)
                {
                    _version = 0;
                }
                var islocalcache = false;
                var apiurl       = await _httpUrlRepository.GetApiUrl(_version);

                var client   = _httpClientFactory.CreateClient(); // 创建http请求
                var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, apiurl));

                response.EnsureSuccessStatusCode();
                var content = await response.Content.ReadAsStringAsync();

                var apiResult = _jsonHepler.DeserializeObject <ApiResult>(content);
                if (apiResult.ErrorCode == "000000" && apiResult.Version > _version)
                {
                    foreach (var kv in apiResult.KV)
                    {
                        Data.AddOrUpdate(kv.Key, kv.Value, (x, y) => kv.Value);
                    }
                    _version     = apiResult.Version;
                    islocalcache = true;
                    // 注册数据更新监听通知
                    foreach (var _lits in DataChangeListenerDictionary.ToList())
                    {
                        _lits.OnDataChange(apiResult.KV);
                    }
                }
                if (islocalcache)
                {
                    _localDataRepository.Set(Data);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"加载配置错误");
                foreach (var kv in _localDataRepository.Get())
                {
                    Data.AddOrUpdate(kv.Key, kv.Value, (x, y) => kv.Value);
                }
            }
        }