App.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Infrastructure.Model;
  2. using Microsoft.Extensions.Options;
  3. using System.Security.Claims;
  4. namespace Infrastructure
  5. {
  6. public static class App
  7. {
  8. /// <summary>
  9. /// 全局配置文件
  10. /// </summary>
  11. public static OptionsSetting OptionsSetting => CatchOrDefault(() => ServiceProvider?.GetService<IOptions<OptionsSetting>>()?.Value);
  12. /// <summary>
  13. /// 服务提供器
  14. /// </summary>
  15. public static IServiceProvider ServiceProvider => InternalApp.ServiceProvider;
  16. /// <summary>
  17. /// 获取请求上下文
  18. /// </summary>
  19. public static HttpContext HttpContext => CatchOrDefault(() => ServiceProvider?.GetService<IHttpContextAccessor>()?.HttpContext);
  20. /// <summary>
  21. /// 获取请求上下文用户
  22. /// </summary>
  23. public static ClaimsPrincipal User => HttpContext?.User;
  24. /// <summary>
  25. /// 获取用户名
  26. /// </summary>
  27. public static string UserName => User?.Identity?.Name;
  28. /// <summary>
  29. /// 获取Web主机环境
  30. /// </summary>
  31. public static IWebHostEnvironment WebHostEnvironment => InternalApp.WebHostEnvironment;
  32. /// <summary>
  33. /// 获取全局配置
  34. /// </summary>
  35. public static IConfiguration Configuration => CatchOrDefault(() => InternalApp.Configuration, new ConfigurationBuilder().Build());
  36. /// <summary>
  37. /// 获取请求生命周期的服务
  38. /// </summary>
  39. /// <typeparam name="TService"></typeparam>
  40. /// <returns></returns>
  41. public static TService GetService<TService>()
  42. where TService : class
  43. {
  44. return GetService(typeof(TService)) as TService;
  45. }
  46. /// <summary>
  47. /// 获取请求生命周期的服务
  48. /// </summary>
  49. /// <param name="type"></param>
  50. /// <returns></returns>
  51. public static object GetService(Type type)
  52. {
  53. return ServiceProvider.GetService(type);
  54. }
  55. /// <summary>
  56. /// 获取请求生命周期的服务
  57. /// </summary>
  58. /// <typeparam name="TService"></typeparam>
  59. /// <returns></returns>
  60. public static TService GetRequiredService<TService>()
  61. where TService : class
  62. {
  63. return GetRequiredService(typeof(TService)) as TService;
  64. }
  65. /// <summary>
  66. /// 获取请求生命周期的服务
  67. /// </summary>
  68. /// <param name="type"></param>
  69. /// <returns></returns>
  70. public static object GetRequiredService(Type type)
  71. {
  72. return ServiceProvider.GetRequiredService(type);
  73. }
  74. /// <summary>
  75. /// 处理获取对象异常问题
  76. /// </summary>
  77. /// <typeparam name="T">类型</typeparam>
  78. /// <param name="action">获取对象委托</param>
  79. /// <param name="defaultValue">默认值</param>
  80. /// <returns>T</returns>
  81. private static T CatchOrDefault<T>(Func<T> action, T defaultValue = null)
  82. where T : class
  83. {
  84. try
  85. {
  86. return action();
  87. }
  88. catch
  89. {
  90. return defaultValue ?? null;
  91. }
  92. }
  93. }
  94. }