From 46e98339480d853fc78a014c34d7ff9fcaf13890 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期四, 05 十二月 2024 14:09:02 +0800 Subject: [PATCH] 产线协议 --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/App.cs | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 203 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/App.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/App.cs" new file mode 100644 index 0000000..2c5653e --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/App.cs" @@ -0,0 +1,203 @@ +锘縰sing 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>鏄惁姝e湪杩愯</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>瑙f瀽鏈嶅姟鎻愪緵鍣�</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 + } +} -- Gitblit v1.9.3