⬆️ 升级包
This commit is contained in:
parent
948b1bcf4e
commit
06f664910b
@ -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 System;
|
||||
using System.Security.Claims;
|
||||
@ -10,16 +12,23 @@ namespace Infrastructure
|
||||
/// <summary>
|
||||
/// 服务提供器
|
||||
/// </summary>
|
||||
public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.ServiceProvider;
|
||||
public static IServiceProvider ServiceProvider => InternalApp.ServiceProvider;
|
||||
/// <summary>
|
||||
/// 获取请求上下文
|
||||
/// </summary>
|
||||
public static HttpContext HttpContext => HttpContextLocal.Current();
|
||||
public static HttpContext HttpContext => CatchOrDefault(() => ServiceProvider?.GetService<IHttpContextAccessor>()?.HttpContext);
|
||||
/// <summary>
|
||||
/// 获取请求上下文用户
|
||||
/// </summary>
|
||||
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>
|
||||
@ -61,5 +70,25 @@ namespace Infrastructure
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,19 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
<Compile Remove="Model\PagedInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Model\PagedInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspectCore.Abstractions" Version="2.3.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
|
||||
namespace Infrastructure
|
||||
@ -15,12 +14,12 @@ namespace Infrastructure
|
||||
/// <summary>
|
||||
/// 全局配置构建器
|
||||
/// </summary>
|
||||
//public static IConfigurationBuilder ConfigurationBuilder;
|
||||
public static IConfiguration Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// 获取Web主机环境
|
||||
/// </summary>
|
||||
//internal static IWebHostEnvironment WebHostEnvironment;
|
||||
public static IWebHostEnvironment WebHostEnvironment;
|
||||
|
||||
/// <summary>
|
||||
/// 获取泛型主机环境
|
||||
|
||||
@ -89,6 +89,8 @@ builder.Services.AddSwaggerConfig();
|
||||
|
||||
var app = builder.Build();
|
||||
InternalApp.ServiceProvider = app.Services;
|
||||
InternalApp.Configuration = builder.Configuration;
|
||||
InternalApp.WebHostEnvironment = app.Environment;
|
||||
//初始化db
|
||||
builder.Services.AddDb(builder.Configuration, app.Environment);
|
||||
|
||||
|
||||
@ -17,13 +17,13 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
|
||||
<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.Filters" Version="7.0.6" />
|
||||
<PackageReference Include="UAParser" Version="3.1.47" />
|
||||
<PackageReference Include="IPTools.China" Version="1.6.0" />
|
||||
<PackageReference Include="NLog" Version="5.1.4" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.2.3" />
|
||||
<PackageReference Include="NLog" Version="5.2.0" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.0" />
|
||||
<PackageReference Include="Mapster" Version="7.3.0" />
|
||||
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.3" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -12,6 +12,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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>
|
||||
</Project>
|
||||
|
||||
@ -8,9 +8,9 @@
|
||||
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" />
|
||||
<PackageReference Include="CSRedisCore" Version="3.8.670" />
|
||||
<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="MiniExcel" Version="1.30.3" />
|
||||
<PackageReference Include="MiniExcel" Version="1.31.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -7,9 +7,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MiniExcel" Version="1.30.3" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84-preview10" />
|
||||
<PackageReference Include="MiniExcel" Version="1.31.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.84" />
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -15,6 +15,6 @@
|
||||
<PackageReference Include="MySqlConnector" Version="2.2.6" />
|
||||
<PackageReference Include="NETCore.Encrypt" Version="2.1.1" />
|
||||
<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>
|
||||
</Project>
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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.Serialization.Json" Version="3.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.1.32210.238
|
||||
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
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZR.Model", "ZR.Model\ZR.Model.csproj", "{B35D73B1-2E22-4636-B88B-10C5E6D8E524}"
|
||||
EndProject
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user