Esempio n. 1
0
        // Add Route Parameter Values to ConfigURL in order of array
        public void SetRouteParametersWithDefault()
        {
            // if route parameters have been supplied, add them as route parameters

            // when the default appId is being used, there will be one parameter with zero length.
            // Assign the default value before adding the URI
            if (_routeParams == null && (_queryParams == null || _queryParams?.Count == 0))
            {
                _routeParams = new string[] { "" }
            }
            ;

            // Add default appId route if there are no query parameters and there is the single empty route parameter (indicating default route)
            if ((_queryParams == null || _queryParams?.Count == 0) && _routeParams.Length == 1 && _routeParams[0] == "")
            {
                _routeParams[0] = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
            }

            //Add route params (supplied or default) to the Configuration URL
            if (_routeParams != null && _routeParams.Length > 0)
            {
                for (int i = 0; i < _routeParams.Length; i++)
                {
                    if (_routeParams[i].Length > 0)
                    {
                        ConfigUrl = ConfigUrl.TrimEnd('/') + "/" + _routeParams[i];
                    }
                }
            }
        }
    }
        /// <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);
        }