/// <summary> /// 加载配置文件 /// </summary> /// <param name="path">配置文件路径</param> public static void LoadConfiguration(string path = null) { if (string.IsNullOrWhiteSpace(path)) { // 默认的App.config会编译成 {程序集名称}.exe.config 或者 {程序集名称}.dll.config #if NET45 path = Path.Combine(BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.config"); #else path = Path.Combine(BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.dll.config"); #endif // WORKAROUND: 测试框架的入口配置文件会导至读取配置文件错误 if (string.IsNullOrWhiteSpace(path) || "testhost" == AppDomain.CurrentDomain.FriendlyName) { path = Path.Combine(BaseDirectory, "app.config"); } } if (path.ToLower().StartsWith("%global%")) { var fileName = path.Substring(8, path.Length - 8); path = string.IsNullOrWhiteSpace(fileName) ? Path.Combine(GlobalDirectory, "app.config") : Path.Combine(GlobalDirectory, fileName); } if (!File.Exists(path)) { // 配置文件不是必要的运行条件, 可以在代码里自行处理连接字符串等 return; } ConfigurationFilePath = path; var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None); RedisConnectString = configuration.AppSettings.Settings[RedisConnectStringKey]?.Value?.Trim(); EmailHost = configuration.AppSettings.Settings[EmailHostKey]?.Value?.Trim(); EmailPort = configuration.AppSettings.Settings[EmailPortKey]?.Value?.Trim(); EmailAccount = configuration.AppSettings.Settings[EmailAccountKey]?.Value?.Trim(); EmailPassword = configuration.AppSettings.Settings[EmailPasswordKey]?.Value?.Trim(); EmailDisplayName = configuration.AppSettings.Settings[EmailDisplayNameKey]?.Value?.Trim(); EnterpiseServiceUrl = configuration.AppSettings.Settings[EnterpiseServiceUrlKey]?.Value?.Trim(); if (!string.IsNullOrWhiteSpace(EnterpiseServiceUrl)) { EnterpiseServiceLogUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}api/v1.0/log"; EnterpiseServiceToken = configuration.AppSettings.Settings[EnterpiseServiceTokenKey]?.Value?.Trim(); EnterpiseServiceStatusApiUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}api/v1.0/taskstatus"; EnterpiseServiceTaskApiUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}api/v1.0/task"; EnterpiseServicePipelineUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}api/v1.0/process"; } EnterpiseServiceLog = !string.IsNullOrWhiteSpace(EnterpiseServiceLogUrl); SystemConnectionStringSettings = configuration.ConnectionStrings.ConnectionStrings[SystemConnectionStringKey]; DataConnectionStringSettings = configuration.ConnectionStrings.ConnectionStrings[DataConnectionStringKey]; SqlEncryptCode = configuration.AppSettings.Settings[SqlEncryptCodeKey]?.Value?.Trim(); }
/// <summary> /// 加载配置文件 /// </summary> /// <param name="path">配置文件路径</param> public static void LoadConfiguration(string path = null) { if (string.IsNullOrWhiteSpace(path)) { path = Path.Combine(BaseDirectory, "app.config"); } if (path.ToLower().StartsWith("%global%")) { var fileName = path.Substring(8, path.Length - 8); path = string.IsNullOrWhiteSpace(fileName) ? Path.Combine(GlobalDirectory, "app.config") : Path.Combine(GlobalDirectory, fileName); } if (!File.Exists(path)) { // 配置文件不是必要的运行条件, 可以在代码里自行处理连接字符串等 Console.WriteLine("Configuration file unfound."); return; } ConfigurationFilePath = path; var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None); RedisConnectString = configuration.AppSettings.Settings[RedisConnectStringKey]?.Value?.Trim(); EmailHost = configuration.AppSettings.Settings[EmailHostKey]?.Value?.Trim(); EmailPort = configuration.AppSettings.Settings[EmailPortKey]?.Value?.Trim(); EmailAccount = configuration.AppSettings.Settings[EmailAccountKey]?.Value?.Trim(); EmailPassword = configuration.AppSettings.Settings[EmailPasswordKey]?.Value?.Trim(); EmailDisplayName = configuration.AppSettings.Settings[EmailDisplayNameKey]?.Value?.Trim(); EnterpiseServiceUrl = configuration.AppSettings.Settings[EnterpiseServiceUrlKey]?.Value?.Trim(); if (!string.IsNullOrWhiteSpace(EnterpiseServiceUrl)) { EnterpiseServiceLogUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}Log/submit"; EnterpiseServiceToken = configuration.AppSettings.Settings[EnterpiseServiceTokenKey]?.Value?.Trim(); EnterpiseServiceStatusUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}TaskStatus/AddOrUpdate"; EnterpiseServiceIncreaseRunningUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}Task/IncreaseRunning"; EnterpiseServiceReduceRunningUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}Task/ReduceRunning"; EnterpiseServicePipelineUrl = $"{EnterpiseServiceUrl}{(EnterpiseServiceUrl.EndsWith("/") ? "" : "/")}Pipeline/Process"; } EnterpiseServiceLog = !string.IsNullOrWhiteSpace(EnterpiseServiceLogUrl); SystemConnectionStringSettings = configuration.ConnectionStrings.ConnectionStrings[SystemConnectionStringKey]; DataConnectionStringSettings = configuration.ConnectionStrings.ConnectionStrings[DataConnectionStringKey]; SqlEncryptCode = configuration.AppSettings.Settings[SqlEncryptCodeKey]?.Value?.Trim(); }