From ed9eacf33f25b9df545f9cf900a95935809c349d Mon Sep 17 00:00:00 2001 From: SikongJueluo Date: Mon, 11 Aug 2025 13:34:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/Program.cs | 10 +++++----- .../src/Services/HttpHdmiVideoStreamService.cs | 16 +++++++++++----- server/src/Services/HttpVideoStreamService.cs | 12 +++++++----- 3 files changed, 23 insertions(+), 15 deletions(-) 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}");