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 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<AsbCloudDbContext>()
.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
.UseNpgsql(connectionString)
.Options;
while (!token.IsCancellationRequested)

View File

@ -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<AsbCloudDbContext>();
optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
optionsBuilder.UseNpgsql(connectionString);
var tempDir = Path.Combine(Path.GetTempPath(), "report");
using var context = new AsbCloudDbContext(optionsBuilder.Options);

View File

@ -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<string, TrackerStat> requests;
public TelemetryTracker(CacheDb cacheDb)
public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration)
{
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
.Options;
using var db = new AsbCloudDbContext(options);