using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using System; using System.Security.Claims; namespace Infrastructure { public static class App { /// /// 服务提供器 /// public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.ServiceProvider; /// /// 获取请求上下文 /// public static HttpContext HttpContext => HttpContextLocal.Current(); /// /// 获取请求上下文用户 /// public static ClaimsPrincipal User => HttpContext?.User; /// /// 获取请求生命周期的服务 /// /// /// public static TService GetService() where TService : class { return GetService(typeof(TService)) as TService; } /// /// 获取请求生命周期的服务 /// /// /// public static object GetService(Type type) { return ServiceProvider.GetService(type); } /// /// 获取请求生命周期的服务 /// /// /// public static TService GetRequiredService() where TService : class { return GetRequiredService(typeof(TService)) as TService; } /// /// 获取请求生命周期的服务 /// /// /// public static object GetRequiredService(Type type) { return ServiceProvider.GetRequiredService(type); } } }