⬆️ 升级包

This commit is contained in:
不做码农 2023-06-20 07:26:25 +08:00
parent 948b1bcf4e
commit 06f664910b
12 changed files with 66 additions and 95 deletions

View File

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Security.Claims; using System.Security.Claims;
@ -10,16 +12,23 @@ namespace Infrastructure
/// <summary> /// <summary>
/// 服务提供器 /// 服务提供器
/// </summary> /// </summary>
public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.ServiceProvider; public static IServiceProvider ServiceProvider => InternalApp.ServiceProvider;
/// <summary> /// <summary>
/// 获取请求上下文 /// 获取请求上下文
/// </summary> /// </summary>
public static HttpContext HttpContext => HttpContextLocal.Current(); public static HttpContext HttpContext => CatchOrDefault(() => ServiceProvider?.GetService<IHttpContextAccessor>()?.HttpContext);
/// <summary> /// <summary>
/// 获取请求上下文用户 /// 获取请求上下文用户
/// </summary> /// </summary>
public static ClaimsPrincipal User => HttpContext?.User; public static ClaimsPrincipal User => HttpContext?.User;
/// <summary>
/// 获取Web主机环境
/// </summary>
public static IWebHostEnvironment WebHostEnvironment => InternalApp.WebHostEnvironment;
/// <summary>
/// 获取全局配置
/// </summary>
public static IConfiguration Configuration => CatchOrDefault(() => InternalApp.Configuration, new ConfigurationBuilder().Build());
/// <summary> /// <summary>
/// 获取请求生命周期的服务 /// 获取请求生命周期的服务
/// </summary> /// </summary>
@ -61,5 +70,25 @@ namespace Infrastructure
{ {
return ServiceProvider.GetRequiredService(type); return ServiceProvider.GetRequiredService(type);
} }
/// <summary>
/// 处理获取对象异常问题
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="action">获取对象委托</param>
/// <param name="defaultValue">默认值</param>
/// <returns>T</returns>
private static T CatchOrDefault<T>(Func<T> action, T defaultValue = null)
where T : class
{
try
{
return action();
}
catch
{
return defaultValue ?? null;
}
}
} }
} }

View File

@ -1,59 +0,0 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
namespace Microsoft.AspNetCore.Http
{
public static class HttpContextLocal
{
private static Func<object> _asyncLocalAccessor;
private static Func<object, object> _holderAccessor;
private static Func<object, HttpContext> _httpContextAccessor;
/// <summary>
/// 获取当前 HttpContext 对象
/// </summary>
/// <returns></returns>
public static HttpContext Current()
{
var asyncLocal = (_asyncLocalAccessor ??= CreateAsyncLocalAccessor())();
if (asyncLocal == null) return null;
var holder = (_holderAccessor ??= CreateHolderAccessor(asyncLocal))(asyncLocal);
if (holder == null) return null;
return (_httpContextAccessor ??= CreateHttpContextAccessor(holder))(holder);
// 创建异步本地访问器
static Func<object> CreateAsyncLocalAccessor()
{
var fieldInfo = typeof(HttpContextAccessor).GetField("_httpContextCurrent", BindingFlags.Static | BindingFlags.NonPublic);
var field = Expression.Field(null, fieldInfo);
return Expression.Lambda<Func<object>>(field).Compile();
}
// 创建常驻 HttpContext 访问器
static Func<object, object> CreateHolderAccessor(object asyncLocal)
{
var holderType = asyncLocal.GetType().GetGenericArguments()[0];
var method = typeof(AsyncLocal<>).MakeGenericType(holderType).GetProperty("Value").GetGetMethod();
var target = Expression.Parameter(typeof(object));
var convert = Expression.Convert(target, asyncLocal.GetType());
var getValue = Expression.Call(convert, method);
return Expression.Lambda<Func<object, object>>(getValue, target).Compile();
}
// 获取 HttpContext 访问器
static Func<object, HttpContext> CreateHttpContextAccessor(object holder)
{
var target = Expression.Parameter(typeof(object));
var convert = Expression.Convert(target, holder.GetType());
var field = Expression.Field(convert, "Context");
var convertAsResult = Expression.Convert(field, typeof(HttpContext));
return Expression.Lambda<Func<object, HttpContext>>(convertAsResult, target).Compile();
}
}
}
}

View File

@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<Compile Remove="Model\PagedInfo.cs" /> <Compile Remove="Model\PagedInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AspectCore.Abstractions" Version="2.3.0" /> <PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,5 @@
using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System; using System;
namespace Infrastructure namespace Infrastructure
@ -15,12 +14,12 @@ namespace Infrastructure
/// <summary> /// <summary>
/// 全局配置构建器 /// 全局配置构建器
/// </summary> /// </summary>
//public static IConfigurationBuilder ConfigurationBuilder; public static IConfiguration Configuration;
/// <summary> /// <summary>
/// 获取Web主机环境 /// 获取Web主机环境
/// </summary> /// </summary>
//internal static IWebHostEnvironment WebHostEnvironment; public static IWebHostEnvironment WebHostEnvironment;
/// <summary> /// <summary>
/// 获取泛型主机环境 /// 获取泛型主机环境

View File

@ -89,6 +89,8 @@ builder.Services.AddSwaggerConfig();
var app = builder.Build(); var app = builder.Build();
InternalApp.ServiceProvider = app.Services; InternalApp.ServiceProvider = app.Services;
InternalApp.Configuration = builder.Configuration;
InternalApp.WebHostEnvironment = app.Environment;
//初始化db //初始化db
builder.Services.AddDb(builder.Configuration, app.Environment); builder.Services.AddDb(builder.Configuration, app.Environment);

View File

@ -17,13 +17,13 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" /> <PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
<PackageReference Include="Lazy.Captcha.Core" Version="2.0.3" /> <PackageReference Include="Lazy.Captcha.Core" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.5" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" /> <PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
<PackageReference Include="UAParser" Version="3.1.47" /> <PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="IPTools.China" Version="1.6.0" /> <PackageReference Include="IPTools.China" Version="1.6.0" />
<PackageReference Include="NLog" Version="5.1.4" /> <PackageReference Include="NLog" Version="5.2.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.2.3" /> <PackageReference Include="NLog.Web.AspNetCore" Version="5.3.0" />
<PackageReference Include="Mapster" Version="7.3.0" /> <PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.3" /> <PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.3" />
</ItemGroup> </ItemGroup>

View File

@ -12,6 +12,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="JinianNet.JNTemplate" Version="2.3.3" /> <PackageReference Include="JinianNet.JNTemplate" Version="2.3.3" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84-preview10" /> <PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -8,9 +8,9 @@
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" /> <PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" />
<PackageReference Include="CSRedisCore" Version="3.8.670" /> <PackageReference Include="CSRedisCore" Version="3.8.670" />
<PackageReference Include="JinianNet.JNTemplate" Version="2.3.3" /> <PackageReference Include="JinianNet.JNTemplate" Version="2.3.3" />
<PackageReference Include="MailKit" Version="4.0.0" /> <PackageReference Include="MailKit" Version="4.1.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="MiniExcel" Version="1.30.3" /> <PackageReference Include="MiniExcel" Version="1.31.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -7,9 +7,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MiniExcel" Version="1.30.3" /> <PackageReference Include="MiniExcel" Version="1.31.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84-preview10" /> <PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -15,6 +15,6 @@
<PackageReference Include="MySqlConnector" Version="2.2.6" /> <PackageReference Include="MySqlConnector" Version="2.2.6" />
<PackageReference Include="NETCore.Encrypt" Version="2.1.1" /> <PackageReference Include="NETCore.Encrypt" Version="2.1.1" />
<PackageReference Include="SqlSugar.IOC" Version="2.0.0" /> <PackageReference Include="SqlSugar.IOC" Version="2.0.0" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84-preview10" /> <PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -6,7 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="NLog" Version="5.1.4" /> <PackageReference Include="NLog" Version="5.2.0" />
<PackageReference Include="Quartz" Version="3.6.2" /> <PackageReference Include="Quartz" Version="3.6.2" />
<PackageReference Include="Quartz.Serialization.Json" Version="3.6.2" /> <PackageReference Include="Quartz.Serialization.Json" Version="3.6.2" />
</ItemGroup> </ItemGroup>

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238 VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZR.Admin.WebApi", "ZR.Admin.WebApi\ZR.Admin.WebApi.csproj", "{E5497BB4-B0C1-4794-9FAE-163F626EC399}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZR.Admin.WebApi", "ZR.Admin.WebApi\ZR.Admin.WebApi.csproj", "{E5497BB4-B0C1-4794-9FAE-163F626EC399}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZR.Model", "ZR.Model\ZR.Model.csproj", "{B35D73B1-2E22-4636-B88B-10C5E6D8E524}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZR.Model", "ZR.Model\ZR.Model.csproj", "{B35D73B1-2E22-4636-B88B-10C5E6D8E524}"
EndProject EndProject