Use configuration.GetConnectionString() in all infrastructure cases.

This commit is contained in:
Фролов 2021-11-17 10:50:57 +05:00
parent 212989b3b6
commit db0b430d93
3 changed files with 13 additions and 9 deletions

View File

@ -7,6 +7,7 @@ using Microsoft.Extensions.Hosting;
using AsbCloudInfrastructure.Services.Cache; using AsbCloudInfrastructure.Services.Cache;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Microsoft.Extensions.Configuration;
namespace AsbCloudInfrastructure.Services.Analysis namespace AsbCloudInfrastructure.Services.Analysis
{ {
@ -14,19 +15,21 @@ namespace AsbCloudInfrastructure.Services.Analysis
{ {
private readonly CacheDb cacheDb; private readonly CacheDb cacheDb;
private readonly ITelemetryTracker telemetryTracker; private readonly ITelemetryTracker telemetryTracker;
private readonly string connectionString;
private readonly TimeSpan period = TimeSpan.FromHours(1); 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.cacheDb = cacheDb;
this.telemetryTracker = telemetryTracker; this.telemetryTracker = telemetryTracker;
connectionString = configuration.GetConnectionString("DefaultConnection");
} }
protected override async Task ExecuteAsync(CancellationToken token = default) protected override async Task ExecuteAsync(CancellationToken token = default)
{ {
var timeToStartAnalysis = DateTime.Now; var timeToStartAnalysis = DateTime.Now;
var options = new DbContextOptionsBuilder<AsbCloudDbContext>() var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") .UseNpgsql(connectionString)
.Options; .Options;
while (!token.IsCancellationRequested) while (!token.IsCancellationRequested)

View File

@ -17,9 +17,9 @@ namespace AsbCloudInfrastructure.Services
public class ReportService : IReportService public class ReportService : IReportService
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly IConfiguration configuration; private readonly string connectionString;
private readonly ITelemetryService telemetryService; private readonly ITelemetryService telemetryService;
private readonly IFileService fileService; //private readonly IFileService fileService;
private readonly IReportsBackgroundQueue queue; private readonly IReportsBackgroundQueue queue;
public ReportService(IAsbCloudDbContext db, IConfiguration configuration, public ReportService(IAsbCloudDbContext db, IConfiguration configuration,
@ -27,9 +27,9 @@ namespace AsbCloudInfrastructure.Services
IReportsBackgroundQueue queue) IReportsBackgroundQueue queue)
{ {
this.db = db; this.db = db;
this.configuration = configuration; this.connectionString = configuration.GetConnectionString("DefaultConnection");
this.telemetryService = telemetryService; this.telemetryService = telemetryService;
this.fileService = fileService; //this.fileService = fileService;
this.queue = queue; this.queue = queue;
ReportCategoryId = db.FileCategories.AsNoTracking() ReportCategoryId = db.FileCategories.AsNoTracking()
.FirstOrDefault(c => .FirstOrDefault(c =>
@ -44,7 +44,7 @@ namespace AsbCloudInfrastructure.Services
var newReportId = queue.EnqueueTask((id) => var newReportId = queue.EnqueueTask((id) =>
{ {
var optionsBuilder = new DbContextOptionsBuilder<AsbCloudDbContext>(); var optionsBuilder = new DbContextOptionsBuilder<AsbCloudDbContext>();
optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection")); optionsBuilder.UseNpgsql(connectionString);
var tempDir = Path.Combine(Path.GetTempPath(), "report"); var tempDir = Path.Combine(Path.GetTempPath(), "report");
using var context = new AsbCloudDbContext(optionsBuilder.Options); using var context = new AsbCloudDbContext(optionsBuilder.Options);

View File

@ -7,6 +7,7 @@ using AsbCloudInfrastructure.Services.Cache;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace AsbCloudInfrastructure.Services namespace AsbCloudInfrastructure.Services
{ {
@ -31,10 +32,10 @@ namespace AsbCloudInfrastructure.Services
private readonly ConcurrentDictionary<string, TrackerStat> requests; private readonly ConcurrentDictionary<string, TrackerStat> requests;
public TelemetryTracker(CacheDb cacheDb) public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration)
{ {
var options = new DbContextOptionsBuilder<AsbCloudDbContext>() var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") .UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
.Options; .Options;
using var db = new AsbCloudDbContext(options); using var db = new AsbCloudDbContext(options);