コード例 #1
0
        /// <summary>
        /// 初始化项目配置参数
        /// </summary>
        /// <returns></returns>
        private Dictionary <string, string> Init()
        {
            Dictionary <string, string> list = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            //本地配置文件
            string localcachejsonpath = Directory.GetCurrentDirectory().Trim('\\') + "\\" + "temp\\" + "config.localcache.json";

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    var result1 = "";
                    using (HttpClient client = new HttpClient())
                    {
                        result1 = client.PostAsync(ConfigUrl.TryReplace("|", "&"), new StringContent("")).Result.Content.ReadAsStringAsync().Result;
                    }
                    if (!string.IsNullOrWhiteSpace(result1))
                    {
                        result1 = new Regex("\"response\":(.*),\"total\"").Match(result1).Groups[1].Value.Trim();
                        if (string.IsNullOrWhiteSpace(result1))
                        {
                            throw new NotFiniteNumberException("获取系统配置信息错误..");
                        }
                        list = result1.FromJson <Dictionary <string, string> >();
                        //写入本地文件
                        if (!_oldResult.Equals(result1))
                        {
                            _oldResult = result1;
                            Task.Factory.StartNew(() =>
                            {
                                IOTool.CreateDirectory(localcachejsonpath);
                                IOTool.WriteTextFile(localcachejsonpath, result1);
                                // ReSharper disable once FunctionNeverReturns
                            });
                        }
                        break;
                        //return list;
                    }
                }
                catch (Exception ex)
                {
                    if (i != 2)
                    {
                        continue;
                    }
                    //加载本地文件
                    if (File.Exists(localcachejsonpath))
                    {
                        try
                        {
                            list = IOTool.ReadTextFile(localcachejsonpath).FromJson <Dictionary <string, string> >();
                        }
                        catch (Exception ex1)
                        {
                            throw new Exception("试图读取统一配置中心出错,读取本地配置文件再次错误,请检查configUrl项配置", ex1);
                        }
                    }
                    else
                    {
                        throw new Exception("加载系统配置错误!", ex);
                    }
                }
            }
            if (list.Count < 1 || !list.ContainsKey("LogConfig.LogSource") || string.IsNullOrWhiteSpace(list["LogConfig.LogSource"]))
            {
                throw new Exception("获取网络配置错误,请检查LogConfig.LogSource,该项为项目标识不能为空");
            }
            return(list);
        }