diff --git a/Admin.NET/Admin.NET.Core/Logging/DatabaseLoggingWriter.cs b/Admin.NET/Admin.NET.Core/Logging/DatabaseLoggingWriter.cs index 8a30b7b6..8409fe67 100644 --- a/Admin.NET/Admin.NET.Core/Logging/DatabaseLoggingWriter.cs +++ b/Admin.NET/Admin.NET.Core/Logging/DatabaseLoggingWriter.cs @@ -188,7 +188,7 @@ public class DatabaseLoggingWriter : IDatabaseLoggingWriter, IDisposable /// private LoggingUserInfo GetUserInfo(LoggingMonitorDto loggingMonitor) { - var userManger = LazyHelper.GetService(); + var userManger = LazyHelper.GetService().Value; LoggingUserInfo result = new(); if (loggingMonitor.AuthorizationClaims != null) { diff --git a/Admin.NET/Admin.NET.Core/Logging/ElasticSearchLoggingWriter.cs b/Admin.NET/Admin.NET.Core/Logging/ElasticSearchLoggingWriter.cs index dc2f943c..d496f9c5 100644 --- a/Admin.NET/Admin.NET.Core/Logging/ElasticSearchLoggingWriter.cs +++ b/Admin.NET/Admin.NET.Core/Logging/ElasticSearchLoggingWriter.cs @@ -42,7 +42,7 @@ public class ElasticSearchLoggingWriter : IDatabaseLoggingWriter, IDisposable return; // 获取当前操作者 - var userManager = LazyHelper.GetService(); + var userManager = LazyHelper.GetService().Value; string account = "", realName = "", userId = "", tenantId = ""; if (loggingMonitor.authorizationClaims != null) { diff --git a/Admin.NET/Admin.NET.Core/Logging/HttpLoggingHandler.cs b/Admin.NET/Admin.NET.Core/Logging/HttpLoggingHandler.cs index 1c7f7b7d..3407a823 100644 --- a/Admin.NET/Admin.NET.Core/Logging/HttpLoggingHandler.cs +++ b/Admin.NET/Admin.NET.Core/Logging/HttpLoggingHandler.cs @@ -38,7 +38,7 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient if (!enabledLog || attr?.IgnoreLog == true) return await base.SendAsync(request, cancellationToken); var stopWatch = Stopwatch.StartNew(); - var userManger = LazyHelper.GetService(); + var userManger = LazyHelper.GetService().Value; var urlList = request.RequestUri?.LocalPath.Split("/") ?? []; var sysLogHttp = new SysLogHttp { diff --git a/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs b/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs index 5e969cc7..8a885c4e 100644 --- a/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Auth/SysAuthService.cs @@ -288,9 +288,11 @@ public class SysAuthService : IDynamicApiController, ITransient user.SysPos ??= await db.Queryable().FirstAsync(u => u.Id == user.PosId); user.SysOrg ??= await db.Queryable().FirstAsync(u => u.Id == user.OrgId); - var permissions = await LazyHelper.GetService().GetUserApiList(user.Id); - var unauthorizedPermissions = await LazyHelper.GetService().GetUnAuthApiList(user.Id); - var orgIds = await LazyHelper.GetService().GetUserOrgIdList(user.Id, user.OrgId); + var sysRoleService = LazyHelper.GetService().Value; + var sysOrgService = LazyHelper.GetService().Value; + var permissions = await sysRoleService.GetUserApiList(user.Id); + var unauthorizedPermissions = await sysRoleService.GetUnAuthApiList(user.Id); + var orgIds = await sysOrgService.GetUserOrgIdList(user.Id, user.OrgId); var roleIds = await db.Queryable().Where(u => u.UserId == user.Id).Select(u => u.RoleId).ToListAsync(); var postIds = await db.Queryable().Where(u => u.UserId == user.Id).Select(u => u.PosId).ToListAsync() ?? []; var maxDataScope = await db.Queryable().Where(u => roleIds.Contains(u.Id)).MinAsync(u => u.DataScope); @@ -321,7 +323,7 @@ public class SysAuthService : IDynamicApiController, ITransient RoleIds = roleIds, Permissions = permissions, UnauthorizedPermissions = unauthorizedPermissions, - AppPermissions = loginMode == LoginModeEnum.APP ? LazyHelper.GetService().GetAppApiList() : null, + AppPermissions = loginMode == LoginModeEnum.APP ? LazyHelper.GetService().Value.GetAppApiList() : null, MaxDataScope = user.AccountType == AccountTypeEnum.SuperAdmin ? DataScopeEnum.All : maxDataScope, ExtProps = App.GetServices().SelectMany(u => u.GetInitExtProps(user)).ToDictionary(u => u.Key, u => u.Value) }); diff --git a/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs b/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs index e6165f1b..8d619ed1 100644 --- a/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs @@ -431,7 +431,8 @@ public class SysCacheService : IDynamicApiController, ISingleton public void Clear() { // 超管用户操作,清空所有缓存 - if (LazyHelper.GetService().SuperAdmin) + var userManager = App.GetService(); + if (userManager.SuperAdmin) { _cacheProvider.Cache.Clear(); Cache.Default.Clear(); diff --git a/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs b/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs index e764237e..a0fd26c9 100644 --- a/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs +++ b/Admin.NET/Admin.NET.Core/Service/User/UserManager.cs @@ -211,7 +211,7 @@ public class UserManager( if (long.TryParse(userId.ToString(), out long tempId)) userId = tempId; else return null; } - LazyHelper.GetService().RefreshSession(userId).GetAwaiter().GetResult(); + LazyHelper.GetService().Value.RefreshSession(userId).GetAwaiter().GetResult(); } return sysCacheService.Get(CacheConst.KeyUserSession + userId); } diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarExtension.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarExtension.cs index 3d07e76e..a735409c 100644 --- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarExtension.cs +++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarExtension.cs @@ -584,7 +584,7 @@ public static class SqlSugarExtension // 使用线程安全的延迟初始化服务实例获取文本缩写 var abbrValue = LazyHelper.GetService() - .GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr }) + .Value.GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr }) .GetAwaiter() .GetResult(); entityInfo.SetValue(abbrValue); @@ -634,7 +634,7 @@ public static class SqlSugarExtension // 使用线程安全的延迟初始化服务实例获取流水号 var serial = LazyHelper.GetService() - .NextSeqNo(attribute.Type, attribute.IsGlobal) + .Value.NextSeqNo(attribute.Type, attribute.IsGlobal) .GetAwaiter() .GetResult(); entityInfo.SetValue(serial); diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarFilter.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarFilter.cs index f038614e..86896c1e 100644 --- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarFilter.cs +++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarFilter.cs @@ -59,7 +59,8 @@ public static class SqlSugarFilter if (string.IsNullOrWhiteSpace(userId)) return; // 获取用户session - var session = LazyHelper.GetService().GetSessionOrRefresh(userId); + var userManager = LazyHelper.GetService().Value; + var session = userManager.GetSessionOrRefresh(userId); if (session == null) return; // 配置用户机构集合缓存 @@ -113,7 +114,8 @@ public static class SqlSugarFilter if (string.IsNullOrWhiteSpace(userId)) return maxDataScope; // 获取用户session - var session = LazyHelper.GetService().GetSessionOrRefresh(userId); + var userManager = LazyHelper.GetService().Value; + var session = userManager.GetSessionOrRefresh(userId); if (session == null) return (int)DataScopeEnum.Self; // 获取用户最大数据范围---仅本人数据 diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs index 8eb2525b..b3a12977 100644 --- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs +++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs @@ -37,7 +37,8 @@ public class SqlSugarRepository : SimpleClient, ISqlSugarRepository whe return; // 若未贴任何表特性或当前未登录或是默认租户Id,则返回默认库连接 - var tenantId = LazyHelper.GetService().TenantId?.ToString(); + var userManager = LazyHelper.GetService().Value; + var tenantId = userManager.TenantId?.ToString(); if (string.IsNullOrWhiteSpace(tenantId) || tenantId == SqlSugarConst.MainConfigId) return; // 根据租户Id切换库连接, 为空则返回默认库连接 diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs index 133bd038..b1ed6f2a 100644 --- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs +++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs @@ -215,7 +215,7 @@ public static class SqlSugarSetup Log.Warning(log); }; - var userManager = LazyHelper.GetService(); + var userManager = LazyHelper.GetService().Value; // 数据审计 dbProvider.Aop.DataExecuting = (oldValue, entityInfo) => { diff --git a/Admin.NET/Admin.NET.Core/Utils/LazyHelper.cs b/Admin.NET/Admin.NET.Core/Utils/LazyHelper.cs index d6fb725a..b54f564b 100644 --- a/Admin.NET/Admin.NET.Core/Utils/LazyHelper.cs +++ b/Admin.NET/Admin.NET.Core/Utils/LazyHelper.cs @@ -6,18 +6,29 @@ namespace Admin.NET.Core; +/// +/// 懒加载帮助类 +/// public class LazyHelper { - private static readonly Dictionary> _cache = new(); + private static readonly ConcurrentDictionary> Cache = new(); /// /// 获取服务 /// /// /// - public static T GetService() where T : class + public static Lazy GetService() where T : class { - if (_cache.TryGetValue(typeof(T), out var lazy)) return (T)lazy.Value; - return (T)(_cache[typeof(T)] = new Lazy(() => App.GetService())).Value; + try + { + var lazy = Cache.GetOrAdd(typeof(T), _ => new Lazy(() => App.GetService())); + return (Lazy)(object)lazy; + } + catch (Exception) + { + Cache.Remove(typeof(T), out _); + return null; + } } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Web.Core/Handlers/JwtHandler.cs b/Admin.NET/Admin.NET.Web.Core/Handlers/JwtHandler.cs index 47f50c3b..6a51e947 100644 --- a/Admin.NET/Admin.NET.Web.Core/Handlers/JwtHandler.cs +++ b/Admin.NET/Admin.NET.Web.Core/Handlers/JwtHandler.cs @@ -91,7 +91,8 @@ namespace Admin.NET.Web.Core } // 验证租户有效期 - var tenantId = LazyHelper.GetService().TenantId?.ToString(); + var userManager = LazyHelper.GetService().Value; + var tenantId = userManager.TenantId?.ToString(); if (!string.IsNullOrWhiteSpace(tenantId)) { var tenant = sysCacheService.Get>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == long.Parse(tenantId)); @@ -117,7 +118,7 @@ namespace Admin.NET.Web.Core /// private static async Task CheckAuthorizeAsync(DefaultHttpContext httpContext) { - var userManager = LazyHelper.GetService(); + var userManager = LazyHelper.GetService().Value; // 排除超管权限判断 if (userManager?.AccountType == AccountTypeEnum.SuperAdmin) return true; diff --git a/Admin.NET/Plugins/Admin.NET.Plugin.ReZero/Service/SuperApiAop.cs b/Admin.NET/Plugins/Admin.NET.Plugin.ReZero/Service/SuperApiAop.cs index f3ddb0eb..29071b63 100644 --- a/Admin.NET/Plugins/Admin.NET.Plugin.ReZero/Service/SuperApiAop.cs +++ b/Admin.NET/Plugins/Admin.NET.Plugin.ReZero/Service/SuperApiAop.cs @@ -61,7 +61,7 @@ public class SuperApiAop : DefaultSuperApiAop var paths = api?.Url?.Split('/'); var actionName = paths?[^1]; - var userManager = LazyHelper.GetService(); + var userManager = LazyHelper.GetService().Value; var apiInfo = new { requestUrl = api?.Url,