fix: 修复数据库无法正常获取信息的问题

This commit is contained in:
SikongJueluo 2025-08-11 13:34:21 +08:00
parent c1d641c20c
commit ed9eacf33f
No known key found for this signature in database
3 changed files with 23 additions and 15 deletions

View File

@ -141,6 +141,11 @@ try
options.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer")); options.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer"));
}); });
// 添加数据库资源管理器服务
builder.Services.AddScoped<Database.AppDataConnection>();
builder.Services.AddScoped<Database.UserManager>();
builder.Services.AddScoped<Database.ResourceManager>();
builder.Services.AddScoped<Database.ExamManager>();
// 添加 HTTP 视频流服务 // 添加 HTTP 视频流服务
builder.Services.AddSingleton<HttpVideoStreamService>(); builder.Services.AddSingleton<HttpVideoStreamService>();
@ -152,11 +157,6 @@ try
builder.Services.AddSingleton<ProgressTrackerService>(); builder.Services.AddSingleton<ProgressTrackerService>();
builder.Services.AddHostedService(provider => provider.GetRequiredService<ProgressTrackerService>()); builder.Services.AddHostedService(provider => provider.GetRequiredService<ProgressTrackerService>());
// 添加数据库资源管理器服务
builder.Services.AddSingleton<Database.AppDataConnection>();
builder.Services.AddSingleton<Database.UserManager>();
builder.Services.AddSingleton<Database.ResourceManager>();
// Application Settings // Application Settings
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

View File

@ -16,16 +16,16 @@ public class HttpHdmiVideoStreamService : BackgroundService
{ {
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly Database.UserManager _userManager; private readonly IServiceProvider _serviceProvider;
private HttpListener? _httpListener; private HttpListener? _httpListener;
private readonly int _serverPort = 4322; private readonly int _serverPort = 4322;
private readonly ConcurrentDictionary<string, HdmiIn> _hdmiInDict = new(); private readonly ConcurrentDictionary<string, HdmiIn> _hdmiInDict = new();
private readonly ConcurrentDictionary<string, CancellationTokenSource> _hdmiInCtsDict = new(); private readonly ConcurrentDictionary<string, CancellationTokenSource> _hdmiInCtsDict = new();
public HttpHdmiVideoStreamService(Database.UserManager userManager) public HttpHdmiVideoStreamService(IServiceProvider serviceProvider)
{ {
_userManager = userManager; _serviceProvider = serviceProvider;
} }
public override async Task StartAsync(CancellationToken cancellationToken) public override async Task StartAsync(CancellationToken cancellationToken)
@ -141,7 +141,10 @@ public class HttpHdmiVideoStreamService : BackgroundService
return hdmiIn; return hdmiIn;
} }
var boardRet = _userManager.GetBoardByID(Guid.Parse(boardId)); using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<Database.UserManager>();
var boardRet = userManager.GetBoardByID(Guid.Parse(boardId));
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
{ {
logger.Error($"Failed to get board with ID {boardId}"); logger.Error($"Failed to get board with ID {boardId}");
@ -367,7 +370,10 @@ public class HttpHdmiVideoStreamService : BackgroundService
/// <returns>返回所有可用的HDMI视频流终端点列表</returns> /// <returns>返回所有可用的HDMI视频流终端点列表</returns>
public List<HdmiVideoStreamEndpoint>? GetAllVideoEndpoints() public List<HdmiVideoStreamEndpoint>? GetAllVideoEndpoints()
{ {
var boards = _userManager.GetAllBoard(); using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<Database.UserManager>();
var boards = userManager.GetAllBoard();
if (boards == null) if (boards == null)
return null; return null;

View File

@ -95,7 +95,7 @@ public class HttpVideoStreamService : BackgroundService
{ {
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly Database.UserManager _userManager; private readonly IServiceProvider _serviceProvider;
private HttpListener? _httpListener; private HttpListener? _httpListener;
private readonly int _serverPort = 4321; private readonly int _serverPort = 4321;
@ -109,12 +109,11 @@ public class HttpVideoStreamService : BackgroundService
private readonly object _usbCameraLock = new object(); private readonly object _usbCameraLock = new object();
#endif #endif
public HttpVideoStreamService(Database.UserManager userManager) public HttpVideoStreamService(IServiceProvider serviceProvider)
{ {
_userManager = userManager; _serviceProvider = serviceProvider;
} }
private Optional<VideoStreamClient> TryGetClient(string boardId) private Optional<VideoStreamClient> TryGetClient(string boardId)
{ {
if (_clientDict.TryGetValue(boardId, out var client)) if (_clientDict.TryGetValue(boardId, out var client))
@ -132,7 +131,10 @@ public class HttpVideoStreamService : BackgroundService
return client; return client;
} }
var boardRet = _userManager.GetBoardByID(Guid.Parse(boardId)); using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<Database.UserManager>();
var boardRet = userManager.GetBoardByID(Guid.Parse(boardId));
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
{ {
logger.Error($"Failed to get board with ID {boardId}"); logger.Error($"Failed to get board with ID {boardId}");