예제 #1
0
        private void LoadAllConnectionStrings()
        {
            try
            {
                var dict = new Dictionary <string, string>();
#if NET45
                var count = ConfigurationManager.ConnectionStrings.Count;
                for (var i = 0; i < count; i++)
                {
                    var key   = ConfigurationManager.ConnectionStrings[i].Name;
                    var value = ConfigurationManager.ConnectionStrings[i].ConnectionString;
                    dict.Add(key, value);
                }
#else
                var connectionStringSection = _configuration.GetSection(nameof(ConfigType.ConnectionStrings));
                var connectionStrings       = _configuration.GetConfiguration(new[] { connectionStringSection });
                foreach (var item in connectionStrings)
                {
                    if (item.Value is string)
                    {
                        dict.Add(item.Key, item.Value.ToString());
                    }
                    else if (item.Value is IList)
                    {
                        var asList = item.Value.To <List <ConnectionStringInfo> >();
                        if (asList != null)
                        {
                            asList.ForEach(m => dict.Add(m.Key, m.Value));
                        }
                    }
                    else
                    {
                        var aS = item.Value.To <ConnectionStringInfo>();
                        if (aS != null)
                        {
                            dict.Add(aS.Key, aS.Value);
                        }
                    }
                }
#endif
                if (dict != null && dict.Any())
                {
                    var configMd5 = dict.ToJsonStr().Md5Encrypt();
                    if (_updateFlagDict.TryGetValue(nameof(ConfigType.ConnectionStrings), out var lastMd5) &&
                        string.Equals(configMd5, lastMd5, StringComparison.OrdinalIgnoreCase))
                    {
                        return;
                    }
                    ConfigCenterContext.SetConnectionStrings(dict);
                    _updateFlagDict.TryAdd(nameof(ConfigType.ConnectionStrings), configMd5);
                    configUpdateTime[nameof(ConfigType.ConnectionStrings)] = DateTime.Now;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Warn($"LoadAllConnectionStrings handler error ,{ex.Message}", ex);
            }
        }
예제 #2
0
        private Tuple <int, string, string> LoadAllConfig()
        {
            Dictionary <string, string>?dict = null;

            // read file from project /Config *.json
            foreach (var file in Directory.GetFiles(ConfigPath).Where(m => CheckFileExtension(Path.GetExtension(m))))
            {
                var fileName = Path.GetFileName(file);
                try
                {
                    if (string.IsNullOrEmpty(fileName))
                    {
                        continue;
                    }
                    if (configUpdateTime.TryGetValue(fileName, out var lastModifyTime) &&
                        lastModifyTime == Directory.GetLastWriteTime(file))
                    {
                        continue;
                    }

                    dict = new Dictionary <string, string>();
                    var text = File.ReadAllText(file);
                    dict.Add(fileName, text);
                    configUpdateTime[fileName] = Directory.GetLastWriteTime(file);
                    _updateFlagDict.AddOrUpdate($"{nameof(ConfigType.JsonConfig)}:{fileName}", k => text.Md5Encrypt(),
                                                (t, l) => text.Md5Encrypt());
                }
                catch (Exception ex)
                {
                    LogHelper.Warn($"LoadJsonConfig({fileName}) handler error ,{ex.Message}", ex);
                }
            }

            if (dict != null && dict.Any())
            {
                ConfigCenterContext.SetJsonConfigList(dict);
            }


            return(new Tuple <int, string, string>(0, "刷新成功", ""));
        }
예제 #3
0
        private Tuple <int, string, string> LoadConfigByFileName(string configName)
        {
            var filePath = Path.Combine(ConfigPath, configName);

            if (!CheckFileExtension(filePath))
            {
                return(new Tuple <int, string, string>(0, "刷新成功", ""));
            }
            var content = string.Empty;

            if (File.Exists(filePath))
            {
                content = File.ReadAllText(filePath);
                if (!string.IsNullOrEmpty(content))
                {
                    ConfigCenterContext.SetJsonConfig(configName, content);
                    configUpdateTime[configName] = Directory.GetLastWriteTime(filePath);
                }
            }

            return(new Tuple <int, string, string>(0, "刷新成功", content));
        }
예제 #4
0
 private void LoadAllDomainSwitch()
 {
     try
     {
         var dict = DomainSwitch.GetAllDomainDict();
         if (dict != null && dict.Any())
         {
             var configMd5 = dict.ToJsonStr().Md5Encrypt();
             if (_updateFlagDict.TryGetValue(nameof(ConfigType.DomainSwitch), out var lastMd5) &&
                 string.Equals(configMd5, lastMd5, StringComparison.OrdinalIgnoreCase))
             {
                 return;
             }
             ConfigCenterContext.SetDomainSwitch(dict);
             _updateFlagDict.TryAdd(nameof(ConfigType.DomainSwitch), configMd5);
             configUpdateTime[nameof(ConfigType.DomainSwitch)] = DateTime.Now;
         }
     }
     catch (Exception ex)
     {
         LogHelper.Warn($"LoadAllDomainSwitch handler error ,{ex.Message}", ex);
     }
 }
예제 #5
0
        /// <summary>
        ///     加载配置
        /// </summary>
        public Tuple <int, string, string> LoadDomainSwitch(string nodeName = "",
                                                            DomainSwitchNodeType nodeType = DomainSwitchNodeType.Unknown)
        {
            lock (LockDW)
            {
                try
                {
                    var configValue = string.Empty;
                    if (!string.IsNullOrEmpty(nodeName) && nodeType != DomainSwitchNodeType.Unknown)
                    {
                        if (nodeType == DomainSwitchNodeType.ServiceUrl)
                        {
                            configValue = DomainSwitch.GetServiceUrl(nodeName);
                        }
                        if (nodeType == DomainSwitchNodeType.SiteHost)
                        {
                            configValue = DomainSwitch.GetSiteHost(nodeName);
                        }

                        var nodePath = $"{nodeType}:{nodeName}";
                        if (!string.IsNullOrEmpty(configValue))
                        {
                            ConfigCenterContext.SetDomainSwitch(nodePath, configValue);
                        }
                        return(new Tuple <int, string, string>(0, "刷新成功", configValue));
                    }

                    LoadAllDomainSwitch();
                    return(new Tuple <int, string, string>(0, "刷新成功", ""));
                }
                catch (Exception ex)
                {
                    LogHelper.Warn($"LoadDomainSwitch({nodeName},{nodeType}) handler error ,{ex.Message}", ex);
                    return(new Tuple <int, string, string>(1, $"LoadDomainSwitch Failed {ex.Message}", ""));
                }
            }
        }