Esempio n. 1
0
        private static void InitAddtionalConfigFiles()
        {
            string config = Convert.ToString(GetSection("AddtionalConfigFiles"));//里面可以是一个数组,指向多个配置文件

            if (!string.IsNullOrEmpty(config))
            {
                string[] items = JsonHelper.ToEntity <string[]>(config);
                if (items != null && items.Length > 0)
                {
                    foreach (string item in items)
                    {
                        string path = AppConfig.RunPath + item;
                        string json = JsonHelper.ReadJson(path);
                        if (string.IsNullOrEmpty(json))
                        {
                            continue;
                        }
                        IOWatch.On(path, delegate(FileSystemEventArgs e)
                        {
                            SetKeyValue(JsonHelper.ReadJson(e.FullPath));
                        });
                        SetKeyValue(json);
                    }
                }
            }
        }
        static ConfigurationManager()
        {
            AppConfig.IsAspNetCore = true;
            ThreadBreak.AddGlobalThread(new ParameterizedThreadStart(RegGB2312));
            string filePath = AppConfig.RunPath + "appsettings.json";

            ReInitConfig(filePath);
            IOWatch.On(filePath, delegate(FileSystemEventArgs e)
            {
                Thread.Sleep(500);
                ReInitConfig(e.FullPath);
            });
        }
        /// <summary>
        /// 开启加载一个配置。
        /// </summary>
        /// <param name="connName">Conn的名称</param>
        /// <param name="connPath">Conn指向的路径</param>
        /// <returns></returns>
        public static string Start(string connName, string connPath)
        {
            lock (o)
            {
                string path = AppConfig.RunPath + connPath;
                string json = JsonHelper.ReadJson(path);
                if (string.IsNullOrEmpty(json))
                {
                    return(connName);
                }
                WatchConfig config = JsonHelper.ToEntity <WatchConfig>(JsonHelper.GetValue(json, connName));
                if (config != null && !string.IsNullOrEmpty(config.Master))
                {
                    AppConfig.SetConn(connName, config.Master);
                    if (!string.IsNullOrEmpty(config.Backup))
                    {
                        AppConfig.SetConn(connName + "_Bak", config.Backup);
                    }
                    if (config.Slave != null && config.Slave.Length > 0)
                    {
                        for (int i = 0; i < config.Slave.Length; i++)
                        {
                            AppConfig.SetConn(connName + "_Slave" + (i + 1), config.Slave[i]);
                        }
                    }
                    if (!watchList.ContainsValue(connPath))
                    {
                        IOWatch.On(path, delegate(FileSystemEventArgs e)
                        {
                            fsy_Changed(e);
                        });
                    }
                    if (!watchList.ContainsKey(connName))
                    {
                        watchList.Add(connName, connPath);
                    }

                    return(config.Master);
                }
            }
            return(connName);
        }
Esempio n. 4
0
        private void ResetHostListByConfig(bool isNeedStartWatch)
        {
            lock (obj2)
            {
                if (string.IsNullOrEmpty(configValue))
                {
                    return;
                }
                string[] hostItems = null;
                if (configValue.EndsWith(".txt") || configValue.EndsWith(".ini"))
                {
                    string path = AppConfig.RunPath + configValue;
                    if (File.Exists(path))
                    {
                        if (isNeedStartWatch)
                        {
                            IOWatch.On(path, delegate(FileSystemEventArgs e) {
                                ResetHostListByConfig(false);
                                if (OnConfigChangedEvent != null)
                                {
                                    OnConfigChangedEvent();
                                }
                            });
                        }

                        hostItems = IOHelper.ReadAllLines(path);
                    }
                }
                else
                {
                    hostItems = configValue.Trim().Split(',');
                }
                if (hostItems == null || hostItems.Length == 0)
                {
                    return;
                }


                HostAddList.Clear();
                HostRemoveList.Clear();
                //开始比较主机配置版本差异
                if (HostList.Count == 0)
                {
                    foreach (string host in hostItems)
                    {
                        string item = host.Trim();
                        if (string.IsNullOrEmpty(item) || item.StartsWith("#"))
                        {
                            continue;
                        }
                        if (!HostAddList.Contains(item))
                        {
                            HostAddList.Add(item);
                            HostList.Add(item);
                        }
                    }
                }
                else
                {
                    foreach (string host in hostItems)
                    {
                        string item = host.Trim();
                        if (string.IsNullOrEmpty(item) || item.StartsWith("#"))
                        {
                            continue;
                        }
                        if (HostList.Contains(item))
                        {
                            HostList.Remove(item);
                        }
                        else if (!HostAddList.Contains(item))
                        {
                            HostAddList.Add(item);
                        }
                    }
                    //剩下的就是要移除的。
                    foreach (string host in HostList.List)
                    {
                        HostRemoveList.Add(host);
                    }
                    HostList.Clear();
                    //把项添加到HostList项。
                    foreach (string host in hostItems)
                    {
                        string item = host.Trim();
                        if (string.IsNullOrEmpty(item) || item.StartsWith("#"))
                        {
                            continue;
                        }
                        if (!HostList.Contains(item))
                        {
                            HostList.Add(item);
                        }
                    }
                }
            }
        }