| | |
| | | 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; |
| | |
| | | /// <summary> |
| | | /// 获取请求上下文 |
| | | /// </summary> |
| | | public static HttpContext HttpContext => RootServices?.GetService<IHttpContextAccessor>()?.HttpContext; |
| | | public static HttpContext? HttpContext => RootServices?.GetService<IHttpContextAccessor>()?.HttpContext; |
| | | |
| | | public static IUser User => GetService<IUser>(); |
| | | |
| | |
| | | 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 = App.GetService(typeof(TService), null, mustBuild) as TService; |
| | | return test; |
| | | object? service = App.GetService(typeof(TService), null, mustBuild); |
| | | return service as TService; |
| | | } |
| | | |
| | | /// <summary>获取请求生存周期的服务</summary> |
| | |
| | | /// <param name="serviceProvider"></param> |
| | | /// <param name="mustBuild"></param> |
| | | /// <returns></returns> |
| | | public static TService GetService<TService>(IServiceProvider serviceProvider, bool mustBuild = true) |
| | | 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="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) |
| | | { |
| | | IServiceProvider? obj2 = (serviceProvider ?? App.GetServiceProvider(type, mustBuild, false)); |
| | | object obj = obj2?.GetService(type); |
| | | object? obj = obj2?.GetService(type); |
| | | return obj; |
| | | } |
| | | |
| | |
| | | /// <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? serviceProvider = null) where TOptions : class, new() |
| | | { |
| | | IOptions<TOptions> service = App.GetService<IOptions<TOptions>>(serviceProvider ?? App.RootServices, false); |
| | | return service?.Value; |
| | |
| | | /// <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 = |
| | | IOptionsMonitor<TOptions>? service = |
| | | App.GetService<IOptionsMonitor<TOptions>>(serviceProvider ?? App.RootServices, false); |
| | | return service?.CurrentValue; |
| | | } |
| | |
| | | /// <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 = App.GetService<IOptionsSnapshot<TOptions>>(serviceProvider, false); |
| | | return service?.Value; |
| | | } |
| | | |