From 2192cdc242bf549db0ee1001e7fa2370d20ba6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 17 Jan 2022 15:33:00 +0500 Subject: [PATCH] Extract BeforeRunHandle method. It runs some procedures like Migrate() on startup. --- AsbCloudDb/Model/AsbCloudDbContext.cs | 4 ++-- AsbCloudDb/Model/IAsbCloudDbContext.cs | 4 +--- AsbCloudInfrastructure/Startup.cs | 26 ++++++++++++++++++++++++++ AsbCloudWebApi/Program.cs | 20 ++++++++++++-------- 4 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 AsbCloudInfrastructure/Startup.cs diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 188e875d..22f94ac4 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -47,14 +47,14 @@ namespace AsbCloudDb.Model public AsbCloudDbContext() : base() { - Database.Migrate(); + //Database.Migrate(); } public AsbCloudDbContext(DbContextOptions options) : base(options) { //Database.SetCommandTimeout(60 * 60 * 2); - Database.Migrate(); + //Database.Migrate(); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index 32c70df7..dc3e5473 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -1,14 +1,12 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudDb.Model { - public interface IAsbCloudDbContext + public interface IAsbCloudDbContext: IDisposable { DbSet Clusters { get; set; } DbSet Companies { get; set; } diff --git a/AsbCloudInfrastructure/Startup.cs b/AsbCloudInfrastructure/Startup.cs new file mode 100644 index 00000000..67f7e14f --- /dev/null +++ b/AsbCloudInfrastructure/Startup.cs @@ -0,0 +1,26 @@ +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services.Cache; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System; + +namespace AsbCloudInfrastructure +{ + public class Startup + { + public static void BeforeRunHandler(IHost host, IConfigurationRoot configuration) + { + using var scope = host.Services.CreateScope(); + var context = scope.ServiceProvider.GetService(); + context.Database.SetCommandTimeout(TimeSpan.FromSeconds(2 * 60)); + + context.Database.Migrate(); + + var wellService = scope.ServiceProvider.GetService(); + wellService.EnshureTimezonesIsSet(); + } + } +} diff --git a/AsbCloudWebApi/Program.cs b/AsbCloudWebApi/Program.cs index 395ec1e7..e33ea74b 100644 --- a/AsbCloudWebApi/Program.cs +++ b/AsbCloudWebApi/Program.cs @@ -1,5 +1,8 @@ +using AsbCloudDb.Model; using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Linq; @@ -10,16 +13,15 @@ namespace AsbCloudWebApi { public static void Main(string[] args) { - //new TraceListenerView(); // to trace mysterious errors - if(args?.Length > 0 ) + IConfigurationRoot configuration = new ConfigurationBuilder() + .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) + .AddJsonFile("appsettings.json") + .Build(); + + if (args?.Length > 0 ) { if (args.Contains("db_init")) { var connectionStringName = "DefaultConnection"; - - IConfigurationRoot configuration = new ConfigurationBuilder() - .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) - .AddJsonFile("appsettings.json") - .Build(); AsbCloudInfrastructure.DependencyInjection.MakeContext(configuration.GetConnectionString(connectionStringName)); Console.WriteLine("Óñïåøíî âûïîëíåíî."); @@ -29,7 +31,9 @@ namespace AsbCloudWebApi return; } - CreateHostBuilder(args).Build().Run(); + var host = CreateHostBuilder(args).Build(); + AsbCloudInfrastructure.Startup.BeforeRunHandler(host, configuration); + host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) =>