1
hutongqing
2024-10-29 9ca96199d92168fe221dda9aba56f55520a561d8
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 ?? 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;
        }