1
hutongqing
2024-09-13 3ca95f10e441ff66d4d62f8dfe202bfb26c3c8e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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 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
    }
}