From 06f664910b43543c3919f2575db06b87637a18d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Tue, 20 Jun 2023 07:26:25 +0800 Subject: [PATCH] =?UTF-8?q?:arrow=5Fup:=20=E5=8D=87=E7=BA=A7=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/App/App.cs | 37 ++++++++++++-- Infrastructure/App/Web/HttpContextLocal.cs | 59 ---------------------- Infrastructure/Infrastructure.csproj | 30 +++++------ Infrastructure/InternalApp.cs | 9 ++-- ZR.Admin.WebApi/Program.cs | 2 + ZR.Admin.WebApi/ZR.Admin.WebApi.csproj | 6 +-- ZR.CodeGenerator/ZR.CodeGenerator.csproj | 2 +- ZR.Common/ZR.Common.csproj | 4 +- ZR.Model/ZR.Model.csproj | 6 +-- ZR.Repository/ZR.Repository.csproj | 2 +- ZR.Tasks/ZR.Tasks.csproj | 2 +- ZRAdmin.sln | 2 +- 12 files changed, 66 insertions(+), 95 deletions(-) delete mode 100644 Infrastructure/App/Web/HttpContextLocal.cs diff --git a/Infrastructure/App/App.cs b/Infrastructure/App/App.cs index 663e8ef..89307f5 100644 --- a/Infrastructure/App/App.cs +++ b/Infrastructure/App/App.cs @@ -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 /// /// 服务提供器 /// - public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.ServiceProvider; + public static IServiceProvider ServiceProvider => InternalApp.ServiceProvider; /// /// 获取请求上下文 /// - public static HttpContext HttpContext => HttpContextLocal.Current(); + public static HttpContext HttpContext => CatchOrDefault(() => ServiceProvider?.GetService()?.HttpContext); /// /// 获取请求上下文用户 /// public static ClaimsPrincipal User => HttpContext?.User; - + /// + /// 获取Web主机环境 + /// + public static IWebHostEnvironment WebHostEnvironment => InternalApp.WebHostEnvironment; + /// + /// 获取全局配置 + /// + public static IConfiguration Configuration => CatchOrDefault(() => InternalApp.Configuration, new ConfigurationBuilder().Build()); /// /// 获取请求生命周期的服务 /// @@ -61,5 +70,25 @@ namespace Infrastructure { return ServiceProvider.GetRequiredService(type); } + + /// + /// 处理获取对象异常问题 + /// + /// 类型 + /// 获取对象委托 + /// 默认值 + /// T + private static T CatchOrDefault(Func action, T defaultValue = null) + where T : class + { + try + { + return action(); + } + catch + { + return defaultValue ?? null; + } + } } } diff --git a/Infrastructure/App/Web/HttpContextLocal.cs b/Infrastructure/App/Web/HttpContextLocal.cs deleted file mode 100644 index 8c0a96d..0000000 --- a/Infrastructure/App/Web/HttpContextLocal.cs +++ /dev/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 _asyncLocalAccessor; - private static Func _holderAccessor; - private static Func _httpContextAccessor; - - /// - /// 获取当前 HttpContext 对象 - /// - /// - 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 CreateAsyncLocalAccessor() - { - var fieldInfo = typeof(HttpContextAccessor).GetField("_httpContextCurrent", BindingFlags.Static | BindingFlags.NonPublic); - var field = Expression.Field(null, fieldInfo); - return Expression.Lambda>(field).Compile(); - } - - // 创建常驻 HttpContext 访问器 - static Func 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>(getValue, target).Compile(); - } - - // 获取 HttpContext 访问器 - static Func 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>(convertAsResult, target).Compile(); - } - } - } -} diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index 16b2da3..c305efe 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -1,19 +1,19 @@ - + + + net7.0 + - - net7.0 - + + + + - - - - - - - - - - - + + + + + + + diff --git a/Infrastructure/InternalApp.cs b/Infrastructure/InternalApp.cs index 78e24d9..3be8301 100644 --- a/Infrastructure/InternalApp.cs +++ b/Infrastructure/InternalApp.cs @@ -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 /// /// 全局配置构建器 /// - //public static IConfigurationBuilder ConfigurationBuilder; + public static IConfiguration Configuration; /// /// 获取Web主机环境 /// - //internal static IWebHostEnvironment WebHostEnvironment; + public static IWebHostEnvironment WebHostEnvironment; /// /// 获取泛型主机环境 diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index 9684a36..232f917 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -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); diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj index 927843a..630d749 100644 --- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj +++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj @@ -17,13 +17,13 @@ - + - - + + diff --git a/ZR.CodeGenerator/ZR.CodeGenerator.csproj b/ZR.CodeGenerator/ZR.CodeGenerator.csproj index a1a2040..8cb72a7 100644 --- a/ZR.CodeGenerator/ZR.CodeGenerator.csproj +++ b/ZR.CodeGenerator/ZR.CodeGenerator.csproj @@ -12,6 +12,6 @@ - + diff --git a/ZR.Common/ZR.Common.csproj b/ZR.Common/ZR.Common.csproj index 2dda42c..942ceb0 100644 --- a/ZR.Common/ZR.Common.csproj +++ b/ZR.Common/ZR.Common.csproj @@ -8,9 +8,9 @@ - + - + diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj index 7efcd39..b5eba23 100644 --- a/ZR.Model/ZR.Model.csproj +++ b/ZR.Model/ZR.Model.csproj @@ -7,9 +7,9 @@ - - - + + + diff --git a/ZR.Repository/ZR.Repository.csproj b/ZR.Repository/ZR.Repository.csproj index 22ea936..a47f054 100644 --- a/ZR.Repository/ZR.Repository.csproj +++ b/ZR.Repository/ZR.Repository.csproj @@ -15,6 +15,6 @@ - + diff --git a/ZR.Tasks/ZR.Tasks.csproj b/ZR.Tasks/ZR.Tasks.csproj index e5ab9d1..1229130 100644 --- a/ZR.Tasks/ZR.Tasks.csproj +++ b/ZR.Tasks/ZR.Tasks.csproj @@ -6,7 +6,7 @@ - + diff --git a/ZRAdmin.sln b/ZRAdmin.sln index 7c14b06..633d2ab 100644 --- a/ZRAdmin.sln +++ b/ZRAdmin.sln @@ -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