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