Ejemplo n.º 1
0
        /// <summary>
        /// 校验配置文件的可用性:加载
        /// TODO配置文件检测没必要每一次都重新加载
        /// </summary>
        /// <param name="configEntity"></param>
        /// <returns></returns>
        protected static DependencyDetail CheckAvailabilityOfConfigForService(FileConfigEntity configEntity)
        {
            DependencyDetail result = new DependencyDetail();

            result.DependencyType = DependencyType.Config;
            result.DependencyName = configEntity.InputFileName;
            StringBuilder message = new StringBuilder();

            //文件不存在的辅助处理
            if (!File.Exists(configEntity.InputFilePath))
            {
                if (!Directory.Exists(configEntity.InputDirectoryPath))
                {
                    Directory.CreateDirectory(configEntity.InputDirectoryPath);
                }
                message.AppendLine("配置文件不存在,配置文件:" + configEntity.InputFileName);
                try
                {
                    if (configEntity is DbConfigEntity)
                    {
                        DbConfigEntity dbConfig = (DbConfigEntity)configEntity;
                        dbConfig.DbConfigItems.Add(new DbConfigItem("OracleSample")
                        {
                            ConnectingString = "", DbType = EDatabaseType.Oracle
                        });
                        dbConfig.DbConfigItems.Add(new DbConfigItem("MySQLSample")
                        {
                            ConnectingString = "", DbType = EDatabaseType.MySQL
                        });
                        dbConfig.DbConfigItems.Add(new DbConfigItem("MSSQLSample")
                        {
                            ConnectingString = "", DbType = EDatabaseType.MSSQL
                        });
                        dbConfig.DbConfigItems.Add(new DbConfigItem("SQLiteSample")
                        {
                            ConnectingString = "", DbType = EDatabaseType.SQLite
                        });
                    }
                    configEntity.Save();
                    message.AppendLine("已创建默认的配置文件,请在配置后重试,文件路径:" + configEntity.InputFilePath);
                }
                catch (Exception ex)
                {
                    message.AppendLine("创建默认的配置文件失败,错误详情:" + ex.ToString());
                }
                result.IsDependencyAvailable = false;
            }
            else
            {
                //可用性检验
                try
                {
                    configEntity.Load();
                    message.AppendLine("配置文件加载成功,配置文件:" + configEntity.InputFileName);
                    result.IsDependencyAvailable = true;
                }
                catch (Exception ex)
                {
                    message.AppendLine("配置文件加载失败,配置文件:" + configEntity.InputFileName);
                    message.AppendLine("错误详情" + ex.ToString());
                    result.IsDependencyAvailable = false;
                }
            }
            result.Message = message.ToString();
            return(result);
        }