From 7ff298c6834275b63b612af49651673689a39660 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期三, 27 十一月 2024 11:33:18 +0800 Subject: [PATCH] 更新 --- WIDESEAWCS_Server/WIDESEAWCS_Core/App.cs | 40 ++++++++++++++++++++-------------------- 1 files changed, 20 insertions(+), 20 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/App.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/App.cs index ebce9ef..3c87168 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Core/App.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/App.cs @@ -41,7 +41,7 @@ public static readonly IEnumerable<Type> EffectiveTypes; /// <summary>浼樺厛浣跨敤App.GetService()鎵嬪姩鑾峰彇鏈嶅姟</summary> - public static IServiceProvider RootServices => IsRun || IsBuild ? InternalApp.RootServices : null; + public static IServiceProvider? RootServices => IsRun || IsBuild ? InternalApp.RootServices : null; /// <summary>鑾峰彇Web涓绘満鐜锛屽锛屾槸鍚︽槸寮�鍙戠幆澧冿紝鐢熶骇鐜绛�</summary> public static IWebHostEnvironment WebHostEnvironment => InternalApp.WebHostEnvironment; @@ -55,9 +55,9 @@ /// <summary> /// 鑾峰彇璇锋眰涓婁笅鏂� /// </summary> - public static HttpContext HttpContext => RootServices?.GetService<IHttpContextAccessor>()?.HttpContext; + public static HttpContext? HttpContext => RootServices?.GetService<IHttpContextAccessor>()?.HttpContext; - public static IUser User => GetService<IUser>(); + public static IUser? User => GetService<IUser>(); #region Service @@ -66,15 +66,15 @@ /// <param name="mustBuild"></param> /// <param name="throwException"></param> /// <returns></returns> - public static IServiceProvider GetServiceProvider(Type serviceType, bool mustBuild = false, bool throwException = true) + public static IServiceProvider? GetServiceProvider(Type serviceType, bool mustBuild = false, bool throwException = true) { - if (App.HostEnvironment == null || App.RootServices != null && + if (HostEnvironment == null || RootServices != null && InternalApp.InternalServices .Where((u => u.ServiceType == (serviceType.IsGenericType ? serviceType.GetGenericTypeDefinition() : serviceType))) .Any((u => u.Lifetime == ServiceLifetime.Singleton))) - return App.RootServices; + return RootServices; //鑾峰彇璇锋眰鐢熷瓨鍛ㄦ湡鐨勬湇鍔� if (HttpContext?.RequestServices != null) @@ -100,9 +100,9 @@ return serviceProvider; } - public static TService GetService<TService>(bool mustBuild = true) where TService : class + public static TService? GetService<TService>(bool mustBuild = true) where TService : class { - TService test = GetService(typeof(TService), null, mustBuild) as TService; + TService? test = GetService(typeof(TService), null, mustBuild) as TService; return test; } @@ -111,17 +111,17 @@ /// <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>(); + public static TService? GetService<TService>(IServiceProvider? serviceProvider, bool mustBuild = true) + where TService : class => (serviceProvider ?? 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) + public static object? GetService(Type type, IServiceProvider? serviceProvider = null, bool mustBuild = true) { - object obj = (serviceProvider ?? App.GetServiceProvider(type, mustBuild, false))?.GetService(type); + object? obj = (serviceProvider ?? GetServiceProvider(type, mustBuild, false))?.GetService(type); return obj; } @@ -158,7 +158,7 @@ public static TOptions GetConfig<TOptions>() where TOptions : class, IConfigurableOptions { - TOptions instance = App.Configuration + TOptions instance = Configuration .GetSection(ConfigurableOptions.GetConfigurationPath(typeof(TOptions))) .Get<TOptions>(); return instance; @@ -168,9 +168,9 @@ /// <typeparam name="TOptions">寮虹被鍨嬮�夐」绫�</typeparam> /// <param name="serviceProvider"></param> /// <returns>TOptions</returns> - public static TOptions GetOptions<TOptions>(IServiceProvider serviceProvider = null) where TOptions : class, new() + public static TOptions? GetOptions<TOptions>(IServiceProvider? rootServices, IServiceProvider serviceProvider = null) where TOptions : class, new() { - IOptions<TOptions> service = App.GetService<IOptions<TOptions>>(serviceProvider ?? App.RootServices, false); + IOptions<TOptions>? service = GetService<IOptions<TOptions>>(serviceProvider ?? rootServices, false); return service?.Value; } @@ -178,11 +178,11 @@ /// <typeparam name="TOptions">寮虹被鍨嬮�夐」绫�</typeparam> /// <param name="serviceProvider"></param> /// <returns>TOptions</returns> - public static TOptions GetOptionsMonitor<TOptions>(IServiceProvider serviceProvider = null) + public static TOptions? GetOptionsMonitor<TOptions>(IServiceProvider serviceProvider = null) where TOptions : class, new() { - IOptionsMonitor<TOptions> service = - App.GetService<IOptionsMonitor<TOptions>>(serviceProvider ?? App.RootServices, false); + IOptionsMonitor<TOptions>? service = + GetService<IOptionsMonitor<TOptions>>(serviceProvider ?? RootServices, false); return service?.CurrentValue; } @@ -190,10 +190,10 @@ /// <typeparam name="TOptions">寮虹被鍨嬮�夐」绫�</typeparam> /// <param name="serviceProvider"></param> /// <returns>TOptions</returns> - public static TOptions GetOptionsSnapshot<TOptions>(IServiceProvider serviceProvider = null) + public static TOptions? GetOptionsSnapshot<TOptions>(IServiceProvider serviceProvider = null) where TOptions : class, new() { - IOptionsSnapshot<TOptions> service = App.GetService<IOptionsSnapshot<TOptions>>(serviceProvider, false); + IOptionsSnapshot<TOptions>? service = GetService<IOptionsSnapshot<TOptions>>(serviceProvider, false); return service?.Value; } -- Gitblit v1.9.3