From 524a05326358a6f5873e806edb4fafe6a6687b2d Mon Sep 17 00:00:00 2001 From: nanxun Date: Fri, 5 Jun 2026 00:02:42 +0800 Subject: [PATCH] fix: PlatformHttpClientFactory uses IServiceScopeFactory to avoid disposed DbContext in danmaku retry PlatformHttpClientFactory was holding a direct ISystemSettingsService reference (Scoped). When the danmaku connection's request scope ended, retry attempts failed with ObjectDisposedException on LiveRecorderDbContext. Changed to use IServiceScopeFactory to create a fresh scope on each CreateAsync call, so the danmaku retry loop always gets a live DbContext. Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com --- .../Services/PlatformHttpClientFactory.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/LiveRecorder.Infrastructure/Services/PlatformHttpClientFactory.cs b/src/LiveRecorder.Infrastructure/Services/PlatformHttpClientFactory.cs index 4bb5009..974a38b 100644 --- a/src/LiveRecorder.Infrastructure/Services/PlatformHttpClientFactory.cs +++ b/src/LiveRecorder.Infrastructure/Services/PlatformHttpClientFactory.cs @@ -5,6 +5,7 @@ using LiveRecorder.Application.Common; using LiveRecorder.Application.Abstractions.Settings; using LiveRecorder.Application.Models.Settings; using LiveRecorder.Domain.Enums; +using Microsoft.Extensions.DependencyInjection; namespace LiveRecorder.Infrastructure.Services; @@ -13,11 +14,11 @@ public sealed class PlatformHttpClientFactory private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(20); private static readonly TimeSpan DefaultConnectTimeout = TimeSpan.FromSeconds(10); - private readonly ISystemSettingsService _systemSettingsService; + private readonly IServiceScopeFactory _serviceScopeFactory; - public PlatformHttpClientFactory(ISystemSettingsService systemSettingsService) + public PlatformHttpClientFactory(IServiceScopeFactory serviceScopeFactory) { - _systemSettingsService = systemSettingsService; + _serviceScopeFactory = serviceScopeFactory; } public async Task CreateAsync( @@ -25,7 +26,9 @@ public sealed class PlatformHttpClientFactory bool forceDirectConnection, CancellationToken cancellationToken = default) { - var settings = await _systemSettingsService.GetAsync(cancellationToken); + using var scope = _serviceScopeFactory.CreateScope(); + var systemSettingsService = scope.ServiceProvider.GetRequiredService(); + var settings = await systemSettingsService.GetAsync(cancellationToken); var proxy = forceDirectConnection ? null : BuildProxy(platform, settings); var handler = new SocketsHttpHandler {