diff --git a/code/Infrastructure/Health/HealthCheckService.cs b/code/Infrastructure/Health/HealthCheckService.cs index 4706d2db..625a03ff 100644 --- a/code/Infrastructure/Health/HealthCheckService.cs +++ b/code/Infrastructure/Health/HealthCheckService.cs @@ -2,6 +2,7 @@ using Common.Enums; using Data; using Infrastructure.Verticals.DownloadClient; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Infrastructure.Health; @@ -12,7 +13,7 @@ namespace Infrastructure.Health; public class HealthCheckService : IHealthCheckService { private readonly ILogger _logger; - private readonly DataContext _dataContext; + private readonly IServiceProvider _serviceProvider; private readonly DownloadServiceFactory _downloadServiceFactory; private readonly Dictionary _healthStatuses = new(); private readonly object _lockObject = new(); @@ -24,11 +25,11 @@ public class HealthCheckService : IHealthCheckService public HealthCheckService( ILogger logger, - DataContext dataContext, + IServiceProvider serviceProvider, DownloadServiceFactory downloadServiceFactory) { _logger = logger; - _dataContext = dataContext; + _serviceProvider = serviceProvider; _downloadServiceFactory = downloadServiceFactory; } @@ -39,8 +40,10 @@ public class HealthCheckService : IHealthCheckService try { + var dataContext = _serviceProvider.GetRequiredService(); + // Get the client configuration - var downloadClientConfig = await _dataContext.DownloadClients + var downloadClientConfig = await dataContext.DownloadClients .Where(x => x.Id == clientId) .FirstOrDefaultAsync(); @@ -104,8 +107,10 @@ public class HealthCheckService : IHealthCheckService try { + var dataContext = _serviceProvider.GetRequiredService(); + // Get all enabled client configurations - var enabledClients = await _dataContext.DownloadClients + var enabledClients = await dataContext.DownloadClients .Where(x => x.Enabled) .Where(x => x.TypeName != DownloadClientTypeName.Usenet) .ToListAsync(); diff --git a/code/Infrastructure/Http/DynamicHttpClientProvider.cs b/code/Infrastructure/Http/DynamicHttpClientProvider.cs index 4d56417a..3c5c5eed 100644 --- a/code/Infrastructure/Http/DynamicHttpClientProvider.cs +++ b/code/Infrastructure/Http/DynamicHttpClientProvider.cs @@ -4,6 +4,7 @@ using Data; using Infrastructure.Http.DynamicHttpClientSystem; using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; namespace Infrastructure.Http; @@ -13,16 +14,16 @@ namespace Infrastructure.Http; public class DynamicHttpClientProvider : IDynamicHttpClientProvider { private readonly ILogger _logger; - private readonly DataContext _dataContext; + private readonly IServiceProvider _serviceProvider; private readonly IDynamicHttpClientFactory _dynamicHttpClientFactory; public DynamicHttpClientProvider( ILogger logger, - DataContext dataContext, + IServiceProvider serviceProvider, IDynamicHttpClientFactory dynamicHttpClientFactory) { _logger = logger; - _dataContext = dataContext; + _serviceProvider = serviceProvider; _dynamicHttpClientFactory = dynamicHttpClientFactory; } @@ -49,7 +50,8 @@ public class DynamicHttpClientProvider : IDynamicHttpClientProvider /// A configured HttpClient instance private HttpClient CreateGenericClient(DownloadClientConfig downloadClientConfig) { - var httpConfig = _dataContext.GeneralConfigs.First(); + var dataContext = _serviceProvider.GetRequiredService(); + var httpConfig = dataContext.GeneralConfigs.First(); var clientName = GetClientName(downloadClientConfig); // Determine the client type based on the download client type diff --git a/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs b/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs index 705c605f..a37ccfcf 100644 --- a/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs +++ b/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs @@ -10,6 +10,7 @@ using Data.Enums; using Infrastructure.Helpers; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Infrastructure.Verticals.ContentBlocker; @@ -17,7 +18,7 @@ namespace Infrastructure.Verticals.ContentBlocker; public sealed class BlocklistProvider { private readonly ILogger _logger; - private readonly DataContext _dataContext; + private readonly IServiceProvider _serviceProvider; private readonly HttpClient _httpClient; private readonly IMemoryCache _cache; private readonly Dictionary _configHashes = new(); @@ -26,13 +27,13 @@ public sealed class BlocklistProvider public BlocklistProvider( ILogger logger, - DataContext dataContext, + IServiceProvider serviceProvider, IMemoryCache cache, IHttpClientFactory httpClientFactory ) { _logger = logger; - _dataContext = dataContext; + _serviceProvider = serviceProvider; _cache = cache; _httpClient = httpClientFactory.CreateClient(Constants.HttpClientWithRetryName); } @@ -41,8 +42,9 @@ public sealed class BlocklistProvider { try { + var dataContext = _serviceProvider.GetRequiredService(); int changedCount = 0; - var queueCleanerConfig = await _dataContext.QueueCleanerConfigs + var queueCleanerConfig = await dataContext.QueueCleanerConfigs .AsNoTracking() .FirstAsync(); bool shouldReload = false;