⬆️ 升级包
This commit is contained in:
@@ -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>
|
||||
/// 获取泛型主机环境
|
||||
|
||||
Reference in New Issue
Block a user