fix 数据库连接切换

This commit is contained in:
2024-02-26 16:31:57 +08:00
parent 3bf049a51e
commit 55b6e957fe
3 changed files with 87 additions and 128 deletions

View File

@@ -27,6 +27,8 @@ public class GainfoService : IGainfoService
private readonly ICurrentUser _currentUser;
private readonly IDevicePermissionService _devicePermissionService;
private readonly RedisCache _redisCache;
private readonly IServiceProvider _serviceProvider;
private readonly IConfiguration _configuration;
private readonly ILogger<GainfoService> _logger;
public GainfoService(IGainfoStore gainfoStore,
@@ -34,6 +36,8 @@ public class GainfoService : IGainfoService
ICurrentUser currentUser,
IDevicePermissionService devicePermissionService,
RedisCache redisCache,
IServiceProvider serviceProvider,
IConfiguration configuration,
ILogger<GainfoService> logger)
{
_gainfoStore = gainfoStore;
@@ -41,6 +45,8 @@ public class GainfoService : IGainfoService
_currentUser = currentUser;
_devicePermissionService = devicePermissionService;
_redisCache = redisCache;
_serviceProvider = serviceProvider;
_configuration = configuration;
_logger = logger;
}
@@ -168,9 +174,7 @@ public class GainfoService : IGainfoService
public async Task<IActionResult> GetFaceAsync(string token)
{
return await Scoped.Request.Value.CreateAsync<IActionResult>(async (sp) =>
{
var timeLimitedDataProtector = sp.GetService<IDataProtectionProvider>()
var timeLimitedDataProtector = _serviceProvider.GetService<IDataProtectionProvider>()
.CreateProtector("down:face")
.ToTimeLimitedDataProtector();
@@ -185,17 +189,7 @@ public class GainfoService : IGainfoService
return new FileContentResult(Convert.FromBase64String(faceCache), "image/png");
}
var configuration = sp.GetService<IConfiguration>();
var faceConnectionString = configuration.GetSection("SqlSugarOptions:FaceConnectionString").Value;
if (faceConnectionString.IsEmpty())
{
_logger.LogWarning("暂不支持获取人脸");
return new NotFoundResult();
}
//todo 切换连接字符串
var dbContext = sp.GetService<IDbContext>();
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
var dbContext = _dbContext.ChangeDb("face");
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == userCode).SingleAsync();
if (face == null)
@@ -209,10 +203,9 @@ public class GainfoService : IGainfoService
}
catch (Exception ex)
{
_logger.LogError(ex, "获取人脸失败:" + ex.Message);
_logger.LogError(ex, "获取人脸失败:{message}", ex.Message);
return new NotFoundResult();
}
});
}
public async Task UploadFaceAsync(GainfoUploadFaceRequest request)
@@ -222,18 +215,7 @@ public class GainfoService : IGainfoService
gainfo.ThrowIfNull("人员档案信息不存在");
await Scoped.Request.Value.CreateAsync(async (sp) =>
{
var configuration = sp.GetService<IConfiguration>();
var faceConnectionString = configuration.GetSection("SqlSugarOptions:FaceConnectionString").Value;
if (faceConnectionString.IsEmpty())
{
throw new UserFriendlyException("暂不支持人脸上传");
}
var dbContext = sp.GetService<IDbContext>();
//todo 切换连接字符串
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
var dbContext = _dbContext.ChangeDb("face");
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == gainfo.Code).SingleAsync();
var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update;
@@ -247,7 +229,7 @@ public class GainfoService : IGainfoService
Face = request.Face
};
await dbContext.InsertAsync(face);
await dbContext.Insertable(face).ExecuteCommandAsync();
}
else
{
@@ -258,12 +240,11 @@ public class GainfoService : IGainfoService
}
face.Face = request.Face;
await dbContext.UpdateAsync(face);
await dbContext.Updateable(face).ExecuteCommandAsync();
}
//生成任务
await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, taskType);
});
}
public async Task UploadFacesAsync(List<GainfoUploadFaceRequest> requests)
@@ -275,14 +256,7 @@ public class GainfoService : IGainfoService
gainfo.ThrowIfNull("人员档案信息不存在");
await Scoped.Request.Value.CreateAsync(async (sp) =>
{
var configuration = sp.GetService<IConfiguration>();
var faceConnectionString = configuration.GetSection("SqlSugarOptions:FaceConnectionString").Value;
var dbContext = sp.GetService<IDbContext>();
//todo 切换连接字符串
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
var dbContext = _dbContext.ChangeDb("face");
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == gainfo.Code).SingleAsync();
var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update;
@@ -296,7 +270,7 @@ public class GainfoService : IGainfoService
Face = request.Face
};
await dbContext.InsertAsync(face);
await dbContext.Insertable(face).ExecuteCommandAsync();
}
else
{
@@ -307,12 +281,11 @@ public class GainfoService : IGainfoService
}
face.Face = request.Face;
await dbContext.UpdateAsync(face);
await dbContext.Updateable(face).ExecuteCommandAsync();
}
//生成任务
await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, taskType);
});
}
}
@@ -321,19 +294,7 @@ public class GainfoService : IGainfoService
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == userCode).SingleAsync();
gainfo.ThrowIfNull($"{userCode}人员信息不存在");
return await Scoped.Request.Value.CreateAsync<string>(async (sp) =>
{
var configuration = sp.GetService<IConfiguration>();
var faceConnectionString = configuration.GetSection($"{nameof(SqlSugarOptions)}:FaceConnectionString").Value;
if (faceConnectionString.IsEmpty())
{
throw new UserFriendlyException("暂不支持获取人脸");
}
var dbContext = sp.GetService<IDbContext>();
//todo 切换连接字符串
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
var dbContext = _dbContext.ChangeDb("face");
var face = await dbContext.Queryable<GainfoFaceEntity>()
.Where(x => x.UserCode == userCode)
@@ -341,19 +302,17 @@ public class GainfoService : IGainfoService
face.ThrowIfNull("人脸照片尚未录入");
var timeLimitedDataProtector = sp.GetService<IDataProtectionProvider>()
var timeLimitedDataProtector = _serviceProvider.GetService<IDataProtectionProvider>()
.CreateProtector("down:face")
.ToTimeLimitedDataProtector();
var faceUrl = configuration.GetSection("FaceUrlOptions:Url").Value;
var limit = configuration.GetSection("FaceUrlOptions:Limit").Get<int>();
var dataProtectionProvider = sp.GetService<IDataProtectionProvider>();
var faceUrl = _configuration.GetSection("FaceUrlOptions:Url").Value;
var limit = _configuration.GetSection("FaceUrlOptions:Limit").Get<int>();
var key = $"face:{userCode}";
var token = timeLimitedDataProtector.Protect(key, TimeSpan.FromSeconds(limit));
return faceUrl + token;
});
}
public async Task<Dictionary<string, string>> GetFaceByUserCodesAsync(GetFaceByUserCodesRequest request)

View File

@@ -1,4 +1,5 @@
using Kurisu.AspNetCore.Startup;
using Kurisu.AspNetCore.Utils;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Startup;
using Microsoft.AspNetCore.DataProtection;
@@ -23,8 +24,8 @@ public class Startup : DefaultStartup
base.ConfigureServices(services);
services.AddRedis();
//todo
var redis = ConnectionMultiplexer.Connect($"192.168.199.30:30115,defaultDatabase:1,password=dlhis123");
var reidConnection = Configuration.GetSection($"{nameof(RedisOptions)}:{nameof(RedisOptions.ConnectionString)}").Value;
var redis = ConnectionMultiplexer.Connect(reidConnection);
services.AddDataProtection()
.SetApplicationName("scheduled")
.PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");

View File

@@ -48,7 +48,6 @@ public class UserService : IUserService
public async Task<List<UserResponse>> GetUsersAsync()
{
//todo tenant
var users = await _dbContext.Queryable<UserEntity>().Select<UserResponse>().ToListAsync();
return users;
}