From db0b430d933c2152309e5c97a43edd36a0012343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Wed, 17 Nov 2021 10:50:57 +0500 Subject: [PATCH] Use configuration.GetConnectionString() in all infrastructure cases. --- .../Analysis/TelemetryAnalyticsBackgroundService.cs | 7 +++++-- AsbCloudInfrastructure/Services/ReportService.cs | 10 +++++----- AsbCloudInfrastructure/Services/TelemetryTracker.cs | 5 +++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs b/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs index 9d41fbb5..a7cf2429 100644 --- a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Hosting; using AsbCloudInfrastructure.Services.Cache; using AsbCloudApp.Services; using AsbCloudDb.Model; +using Microsoft.Extensions.Configuration; namespace AsbCloudInfrastructure.Services.Analysis { @@ -14,19 +15,21 @@ namespace AsbCloudInfrastructure.Services.Analysis { private readonly CacheDb cacheDb; private readonly ITelemetryTracker telemetryTracker; + private readonly string connectionString; private readonly TimeSpan period = TimeSpan.FromHours(1); - public TelemetryAnalyticsBackgroundService(CacheDb cacheDb, ITelemetryTracker telemetryTracker) + public TelemetryAnalyticsBackgroundService(CacheDb cacheDb, ITelemetryTracker telemetryTracker, IConfiguration configuration) { this.cacheDb = cacheDb; this.telemetryTracker = telemetryTracker; + connectionString = configuration.GetConnectionString("DefaultConnection"); } protected override async Task ExecuteAsync(CancellationToken token = default) { var timeToStartAnalysis = DateTime.Now; var options = new DbContextOptionsBuilder() - .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") + .UseNpgsql(connectionString) .Options; while (!token.IsCancellationRequested) diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index 931fd344..2ce4e54a 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -17,9 +17,9 @@ namespace AsbCloudInfrastructure.Services public class ReportService : IReportService { private readonly IAsbCloudDbContext db; - private readonly IConfiguration configuration; + private readonly string connectionString; private readonly ITelemetryService telemetryService; - private readonly IFileService fileService; + //private readonly IFileService fileService; private readonly IReportsBackgroundQueue queue; public ReportService(IAsbCloudDbContext db, IConfiguration configuration, @@ -27,9 +27,9 @@ namespace AsbCloudInfrastructure.Services IReportsBackgroundQueue queue) { this.db = db; - this.configuration = configuration; + this.connectionString = configuration.GetConnectionString("DefaultConnection"); this.telemetryService = telemetryService; - this.fileService = fileService; + //this.fileService = fileService; this.queue = queue; ReportCategoryId = db.FileCategories.AsNoTracking() .FirstOrDefault(c => @@ -44,7 +44,7 @@ namespace AsbCloudInfrastructure.Services var newReportId = queue.EnqueueTask((id) => { var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection")); + optionsBuilder.UseNpgsql(connectionString); var tempDir = Path.Combine(Path.GetTempPath(), "report"); using var context = new AsbCloudDbContext(optionsBuilder.Options); diff --git a/AsbCloudInfrastructure/Services/TelemetryTracker.cs b/AsbCloudInfrastructure/Services/TelemetryTracker.cs index 4c05c883..19140309 100644 --- a/AsbCloudInfrastructure/Services/TelemetryTracker.cs +++ b/AsbCloudInfrastructure/Services/TelemetryTracker.cs @@ -7,6 +7,7 @@ using AsbCloudInfrastructure.Services.Cache; using Microsoft.EntityFrameworkCore; using System.Collections.Concurrent; using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; namespace AsbCloudInfrastructure.Services { @@ -31,10 +32,10 @@ namespace AsbCloudInfrastructure.Services private readonly ConcurrentDictionary requests; - public TelemetryTracker(CacheDb cacheDb) + public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration) { var options = new DbContextOptionsBuilder() - .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") + .UseNpgsql(configuration.GetConnectionString("DefaultConnection")) .Options; using var db = new AsbCloudDbContext(options);