diff --git a/server/Program.cs b/server/Program.cs index 3116ce5..b3c015e 100644 --- a/server/Program.cs +++ b/server/Program.cs @@ -141,6 +141,11 @@ try options.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer")); }); + // 添加数据库资源管理器服务 + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); // 添加 HTTP 视频流服务 builder.Services.AddSingleton(); @@ -152,11 +157,6 @@ try builder.Services.AddSingleton(); builder.Services.AddHostedService(provider => provider.GetRequiredService()); - // 添加数据库资源管理器服务 - builder.Services.AddSingleton(); - builder.Services.AddSingleton(); - builder.Services.AddSingleton(); - // Application Settings var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/server/src/Services/HttpHdmiVideoStreamService.cs b/server/src/Services/HttpHdmiVideoStreamService.cs index 1e762ee..56ac0d8 100644 --- a/server/src/Services/HttpHdmiVideoStreamService.cs +++ b/server/src/Services/HttpHdmiVideoStreamService.cs @@ -16,16 +16,16 @@ public class HttpHdmiVideoStreamService : BackgroundService { private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - private readonly Database.UserManager _userManager; + private readonly IServiceProvider _serviceProvider; private HttpListener? _httpListener; private readonly int _serverPort = 4322; private readonly ConcurrentDictionary _hdmiInDict = new(); private readonly ConcurrentDictionary _hdmiInCtsDict = new(); - public HttpHdmiVideoStreamService(Database.UserManager userManager) + public HttpHdmiVideoStreamService(IServiceProvider serviceProvider) { - _userManager = userManager; + _serviceProvider = serviceProvider; } public override async Task StartAsync(CancellationToken cancellationToken) @@ -141,7 +141,10 @@ public class HttpHdmiVideoStreamService : BackgroundService return hdmiIn; } - var boardRet = _userManager.GetBoardByID(Guid.Parse(boardId)); + using var scope = _serviceProvider.CreateScope(); + var userManager = scope.ServiceProvider.GetRequiredService(); + + var boardRet = userManager.GetBoardByID(Guid.Parse(boardId)); if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) { logger.Error($"Failed to get board with ID {boardId}"); @@ -367,7 +370,10 @@ public class HttpHdmiVideoStreamService : BackgroundService /// 返回所有可用的HDMI视频流终端点列表 public List? GetAllVideoEndpoints() { - var boards = _userManager.GetAllBoard(); + using var scope = _serviceProvider.CreateScope(); + var userManager = scope.ServiceProvider.GetRequiredService(); + + var boards = userManager.GetAllBoard(); if (boards == null) return null; diff --git a/server/src/Services/HttpVideoStreamService.cs b/server/src/Services/HttpVideoStreamService.cs index a237bf5..2a14caf 100644 --- a/server/src/Services/HttpVideoStreamService.cs +++ b/server/src/Services/HttpVideoStreamService.cs @@ -95,7 +95,7 @@ public class HttpVideoStreamService : BackgroundService { private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - private readonly Database.UserManager _userManager; + private readonly IServiceProvider _serviceProvider; private HttpListener? _httpListener; private readonly int _serverPort = 4321; @@ -109,12 +109,11 @@ public class HttpVideoStreamService : BackgroundService private readonly object _usbCameraLock = new object(); #endif - public HttpVideoStreamService(Database.UserManager userManager) + public HttpVideoStreamService(IServiceProvider serviceProvider) { - _userManager = userManager; + _serviceProvider = serviceProvider; } - private Optional TryGetClient(string boardId) { if (_clientDict.TryGetValue(boardId, out var client)) @@ -132,7 +131,10 @@ public class HttpVideoStreamService : BackgroundService return client; } - var boardRet = _userManager.GetBoardByID(Guid.Parse(boardId)); + using var scope = _serviceProvider.CreateScope(); + var userManager = scope.ServiceProvider.GetRequiredService(); + + var boardRet = userManager.GetBoardByID(Guid.Parse(boardId)); if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) { logger.Error($"Failed to get board with ID {boardId}");