using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; namespace Znyc.Recruitment.Admin.Commons.Core.App { /// /// 全局应用类 /// public static class App { /// /// 全局配置选项 /// public static IConfiguration Configuration; /// /// 应用服务 /// public static IServiceCollection Services; /// /// 全局配置构建器 /// private static IConfigurationBuilder ConfigurationBuilder; /// /// 私有环境变量,避免重复解析 /// private static IWebHostEnvironment _webHostEnvironment; /// /// 应用有效程序集 /// public static readonly IEnumerable Assemblies; /// /// 有效程序集类型 /// public static readonly IEnumerable EffectiveTypes; /// /// 构造函数 /// static App() { // 编译配置 // Configuration =ConfigurationBuilder.Build(); } /// /// 应用环境,如,是否是开发环境,生产环境等 /// public static IWebHostEnvironment WebHostEnvironment => _webHostEnvironment ??= GetService(); /// /// 服务提供器 /// public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? Services.BuildServiceProvider(); /// /// 获取请求上下文 /// public static HttpContext HttpContext => HttpContextLocal.Current(); /// /// 获取请求生命周期的服务 /// /// /// public static TService GetService() where TService : class { return GetService(typeof(TService)) as TService; } /// /// 获取请求生命周期的服务 /// /// /// public static object GetService(Type type) { return ServiceProvider.GetService(type); } /// /// 获取请求生命周期的服务 /// /// /// public static TService GetRequiredService() where TService : class { return GetRequiredService(typeof(TService)) as TService; } /// /// 获取请求生命周期的服务 /// /// /// public static object GetRequiredService(Type type) { return ServiceProvider.GetRequiredService(type); } /// /// 获取选项 /// /// 强类型选项类 /// TOptions public static TOptions GetOptions() where TOptions : class, new() { return GetService>()?.Value; } /// /// 获取选项 /// /// 强类型选项类 /// TOptions public static TOptions GetOptionsMonitor() where TOptions : class, new() { return GetService>()?.CurrentValue; } /// /// 获取选项 /// /// 强类型选项类 /// TOptions public static TOptions GetOptionsSnapshot() where TOptions : class, new() { return GetService>()?.Value; } #region /// /// 添加配置文件 /// /// /// internal static void AddConfigureFiles(IConfigurationBuilder config, IHostEnvironment env) { // 读取忽略的配置文件 string[] ignoreConfigurationFiles = config.Build() .GetSection("IgnoreConfigurationFiles").Get() ?? Array.Empty(); // 加载配置 AutoAddJsonFiles(config, env, ignoreConfigurationFiles); AutoAddXmlFiles(config, env, ignoreConfigurationFiles); // 存储配置 ConfigurationBuilder = config; } /// /// 自动加载自定义 .json 配置文件 /// /// /// /// public static void AutoAddJsonFiles(IConfigurationBuilder config, IHostEnvironment env, string[] ignoreConfigurationFiles) { // 获取程序目录下的所有配置文件 IEnumerable jsonFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.json", SearchOption.TopDirectoryOnly) .Union( Directory.GetFiles(Directory.GetCurrentDirectory(), "*.json", SearchOption.TopDirectoryOnly) ) .Where(u => !excludeJsons.Contains(Path.GetFileName(u)) && !ignoreConfigurationFiles.Contains(Path.GetFileName(u)) && !runtimeJsonSuffixs.Any(j => u.EndsWith(j))); if (!jsonFiles.Any()) { return; } // 获取环境变量名 string envName = env.EnvironmentName; List envFiles = new List(); // 自动加载配置文件 foreach (string jsonFile in jsonFiles) { // 处理带环境的配置文件 if (Path.GetFileNameWithoutExtension(jsonFile).EndsWith($".{envName}")) { envFiles.Add(jsonFile); continue; } config.AddJsonFile(jsonFile, true, true); } // 配置带环境的配置文件 envFiles.ForEach(u => config.AddJsonFile(u, true, true)); } /// /// 自动加载自定义 .xml 配置文件 /// /// /// /// public static void AutoAddXmlFiles(IConfigurationBuilder config, IHostEnvironment env, string[] ignoreConfigurationFiles) { // 获取程序目录下的所有配置文件,必须以 .config.xml 结尾 IEnumerable xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.xml", SearchOption.TopDirectoryOnly) .Union( Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xml", SearchOption.TopDirectoryOnly) ) .Where(u => !ignoreConfigurationFiles.Contains(Path.GetFileName(u)) && u.EndsWith(".config.xml", StringComparison.OrdinalIgnoreCase)); if (!xmlFiles.Any()) { return; } // 获取环境变量名 string envName = env.EnvironmentName; List envFiles = new List(); // 自动加载配置文件 foreach (string xmlFile in xmlFiles) { // 处理带环境的配置文件 if (Path.GetFileNameWithoutExtension(xmlFile).EndsWith($".{envName}.config")) { envFiles.Add(xmlFile); continue; } config.AddXmlFile(xmlFile, true, true); } // 配置带环境的配置文件 envFiles.ForEach(u => config.AddXmlFile(u, true, true)); } /// /// 默认排除配置项 /// private static readonly string[] excludeJsons = { "appsettings.json", "appsettings.Development.json", "appsettings.Production.json", "bundleconfig.json", "bundleconfig.Development.json", "bundleconfig.Production.json", "compilerconfig.json", "compilerconfig.Development.json", "compilerconfig.Production.json" }; /// /// 排除运行时 Json 后缀 /// private static readonly string[] runtimeJsonSuffixs = { "deps.json", "runtimeconfig.dev.json", "runtimeconfig.prod.json", "runtimeconfig.json" }; #endregion } }