123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using Infrastructure.Model;
- using Microsoft.Extensions.Options;
- using System.Security.Claims;
- namespace Infrastructure
- {
- public static class App
- {
-
-
-
- public static OptionsSetting OptionsSetting => CatchOrDefault(() => ServiceProvider?.GetService<IOptions<OptionsSetting>>()?.Value);
-
-
-
- public static IServiceProvider ServiceProvider => InternalApp.ServiceProvider;
-
-
-
- public static HttpContext HttpContext => CatchOrDefault(() => ServiceProvider?.GetService<IHttpContextAccessor>()?.HttpContext);
-
-
-
- public static ClaimsPrincipal User => HttpContext?.User;
-
-
-
- public static string UserName => User?.Identity?.Name;
-
-
-
- public static IWebHostEnvironment WebHostEnvironment => InternalApp.WebHostEnvironment;
-
-
-
- public static IConfiguration Configuration => CatchOrDefault(() => InternalApp.Configuration, new ConfigurationBuilder().Build());
-
-
-
-
-
- public static TService GetService<TService>()
- where TService : class
- {
- return GetService(typeof(TService)) as TService;
- }
-
-
-
-
-
- public static object GetService(Type type)
- {
- return ServiceProvider.GetService(type);
- }
-
-
-
-
-
- public static TService GetRequiredService<TService>()
- where TService : class
- {
- return GetRequiredService(typeof(TService)) as TService;
- }
-
-
-
-
-
- public static object GetRequiredService(Type type)
- {
- return ServiceProvider.GetRequiredService(type);
- }
-
-
-
-
-
-
-
- private static T CatchOrDefault<T>(Func<T> action, T defaultValue = null)
- where T : class
- {
- try
- {
- return action();
- }
- catch
- {
- return defaultValue ?? null;
- }
- }
- }
- }
|