Blog.Core/Blog.Core.Common/App.cs
LemonNoCry 966db5ec8c
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
.NET Core / build (push) Has been cancelled
🎨 优化调整缓存性能
1.调整获取所有Key的方式,原有写法是维护一个AllKey的集合,在长时间运行后Key越来越多性能越来越低,故此优化,直接使用Redis原生API
2.优化App.GetServiceProvider的逻辑,如果是单例则直接返回RootServices
3.优化IpPolicyRateLimitSetup,使用分布式缓存,而不是MemoryCache
4.优化MiniProfilerSetup,增加注册使用Redis
5.优化DataProtectionSetup,在不使用Redis时,默认保存在临时文件夹
2024-07-18 17:47:31 +08:00

192 lines
6.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Blog.Core.Common.Core;
using Blog.Core.Common.Extensions;
using Blog.Core.Common.HttpContextUser;
using Blog.Core.Common.Option.Core;
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;
namespace Blog.Core.Common;
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 (HttpContext?.RequestServices != null)
return HttpContext.RequestServices;
if (App.RootServices != null)
{
return RootServices;
}
//单例
if (InternalApp.InternalServices
.Where(u => u.ServiceType == (serviceType.IsGenericType ? serviceType.GetGenericTypeDefinition() : serviceType))
.Any(u => u.Lifetime == ServiceLifetime.Singleton))
{
return RootServices ?? InternalApp.InternalServices.BuildServiceProvider();
}
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 =>
App.GetService(typeof(TService), null, mustBuild) as TService;
/// <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) =>
(serviceProvider ?? App.GetServiceProvider(type, mustBuild, false))?.GetService(type);
#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
}