¶Ô±ÈÐÂÎļþ |
| | |
| | | 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 WIDESEA_Core.Core; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.HttpContextUser; |
| | | |
| | | namespace WIDESEA_Core |
| | | { |
| | | public class App |
| | | { |
| | | static App() |
| | | { |
| | | EffectiveTypes = Assemblies.SelectMany(GetTypes); |
| | | } |
| | | |
| | | private static bool _isRun; |
| | | |
| | | /// <summary> |
| | | /// æææ |
| | | /// </summary> |
| | | public static DateTime? ExpDateTime = null; |
| | | |
| | | /// <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 (App.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 = App.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) |
| | | { |
| | | IServiceProvider? obj2 = (serviceProvider ?? App.GetServiceProvider(type, mustBuild, false)); |
| | | object obj = obj2?.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 |
| | | } |
| | | } |