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.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Core.Core; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.HttpContextUser; namespace WIDESEAWCS_Core { public class App { static App() { EffectiveTypes = Assemblies.SelectMany(GetTypes); } private static bool _isRun; /// <summary>æ˜¯å¦æ£åœ¨è¿è¡Œ</summary> public static bool IsBuild { get; set; } public static bool IsRun { get => _isRun; set => _isRun = IsBuild = value; } /// <summary>应用有效程åºé›†</summary> public static readonly IEnumerable<Assembly> Assemblies = RuntimeExtension.GetAllAssemblies(); /// <summary>有效程åºé›†ç±»åž‹</summary> public static readonly IEnumerable<Type> EffectiveTypes; /// <summary>优先使用App.GetService()æ‰‹åŠ¨èŽ·å–æœåŠ¡</summary> public static IServiceProvider RootServices => IsRun || IsBuild ? InternalApp.RootServices : null; /// <summary>获å–Webä¸»æœºçŽ¯å¢ƒï¼Œå¦‚ï¼Œæ˜¯å¦æ˜¯å¼€å‘环境,生产环境ç‰</summary> public static IWebHostEnvironment WebHostEnvironment => InternalApp.WebHostEnvironment; /// <summary>èŽ·å–æ³›åž‹ä¸»æœºçŽ¯å¢ƒï¼Œå¦‚ï¼Œæ˜¯å¦æ˜¯å¼€å‘环境,生产环境ç‰</summary> public static IHostEnvironment HostEnvironment => InternalApp.HostEnvironment; /// <summary>全局é…置选项</summary> public static IConfiguration Configuration => InternalApp.Configuration; /// <summary> /// 获å–请求上下文 /// </summary> public static HttpContext HttpContext => RootServices?.GetService<IHttpContextAccessor>()?.HttpContext; public static IUser User => GetService<IUser>(); #region Service /// <summary>è§£æžæœåŠ¡æä¾›å™¨</summary> /// <param name="serviceType"></param> /// <param name="mustBuild"></param> /// <param name="throwException"></param> /// <returns></returns> public static IServiceProvider GetServiceProvider(Type serviceType, bool mustBuild = false, bool throwException = true) { if (App.HostEnvironment == null || App.RootServices != null && InternalApp.InternalServices .Where((u => u.ServiceType == (serviceType.IsGenericType ? serviceType.GetGenericTypeDefinition() : serviceType))) .Any((u => u.Lifetime == ServiceLifetime.Singleton))) return App.RootServices; //获å–请求生å˜å‘¨æœŸçš„æœåŠ¡ if (HttpContext?.RequestServices != null) return HttpContext.RequestServices; if (RootServices != null) { IServiceScope scope = RootServices.CreateScope(); return scope.ServiceProvider; } if (mustBuild) { if (throwException) { throw new ApplicationException("当å‰ä¸å¯ç”¨ï¼Œå¿…é¡»è¦ç‰åˆ° WebApplication BuildåŽ"); } return default; } ServiceProvider serviceProvider = InternalApp.InternalServices.BuildServiceProvider(); return serviceProvider; } public static TService GetService<TService>(bool mustBuild = true) where TService : class { TService test = GetService(typeof(TService), null, mustBuild) as TService; return test; } /// <summary>获å–请求生å˜å‘¨æœŸçš„æœåŠ¡</summary> /// <typeparam name="TService"></typeparam> /// <param name="serviceProvider"></param> /// <param name="mustBuild"></param> /// <returns></returns> public static TService GetService<TService>(IServiceProvider serviceProvider, bool mustBuild = true) where TService : class => (serviceProvider ?? App.GetServiceProvider(typeof(TService), mustBuild, false))?.GetService<TService>(); /// <summary>获å–请求生å˜å‘¨æœŸçš„æœåŠ¡</summary> /// <param name="type"></param> /// <param name="serviceProvider"></param> /// <param name="mustBuild"></param> /// <returns></returns> public static object GetService(Type type, IServiceProvider serviceProvider = null, bool mustBuild = true) { object obj = (serviceProvider ?? GetServiceProvider(type, mustBuild, false))?.GetService(type); return obj; } #endregion #region private /// <summary>åŠ è½½ç¨‹åºé›†ä¸çš„æ‰€æœ‰ç±»åž‹</summary> /// <param name="ass"></param> /// <returns></returns> private static IEnumerable<Type> GetTypes(Assembly ass) { Type[] source = Array.Empty<Type>(); try { source = ass.GetTypes(); } catch { //$@"Error load `{ass.FullName}` assembly.".WriteErrorLine(); } return source.Where(u => u.IsPublic); } #endregion #region Options /// <summary>获å–é…ç½®</summary> /// <typeparam name="TOptions">强类型选项类</typeparam> /// <returns>TOptions</returns> public static TOptions GetConfig<TOptions>() where TOptions : class, IConfigurableOptions { TOptions instance = App.Configuration .GetSection(ConfigurableOptions.GetConfigurationPath(typeof(TOptions))) .Get<TOptions>(); return instance; } /// <summary>获å–选项</summary> /// <typeparam name="TOptions">强类型选项类</typeparam> /// <param name="serviceProvider"></param> /// <returns>TOptions</returns> public static TOptions GetOptions<TOptions>(IServiceProvider serviceProvider = null) where TOptions : class, new() { IOptions<TOptions> service = App.GetService<IOptions<TOptions>>(serviceProvider ?? App.RootServices, false); return service?.Value; } /// <summary>获å–选项</summary> /// <typeparam name="TOptions">强类型选项类</typeparam> /// <param name="serviceProvider"></param> /// <returns>TOptions</returns> public static TOptions GetOptionsMonitor<TOptions>(IServiceProvider serviceProvider = null) where TOptions : class, new() { IOptionsMonitor<TOptions> service = App.GetService<IOptionsMonitor<TOptions>>(serviceProvider ?? App.RootServices, false); return service?.CurrentValue; } /// <summary>获å–选项</summary> /// <typeparam name="TOptions">强类型选项类</typeparam> /// <param name="serviceProvider"></param> /// <returns>TOptions</returns> public static TOptions GetOptionsSnapshot<TOptions>(IServiceProvider serviceProvider = null) where TOptions : class, new() { IOptionsSnapshot<TOptions> service = App.GetService<IOptionsSnapshot<TOptions>>(serviceProvider, false); return service?.Value; } #endregion } }