@@ -43,8 +43,8 @@ namespace eFormAPI.Web;
4343using Hosting . Helpers ;
4444using Hosting . Helpers . DbOptions ;
4545using Hosting . Settings ;
46- using Microsoft . AspNetCore ;
4746using Microsoft . AspNetCore . Hosting ;
47+ using Microsoft . Extensions . Hosting ;
4848using Microsoft . Extensions . Configuration ;
4949using Microsoft . Extensions . DependencyInjection ;
5050using Microsoft . Extensions . Logging ;
@@ -147,9 +147,9 @@ public static void Stop()
147147
148148 // public static ReloadDbConfiguration ReloadDbConfigurationDelegate { get; set; }
149149
150- public static async void LoadNavigationMenuEnabledPlugins ( IWebHost webHost )
150+ public static async void LoadNavigationMenuEnabledPlugins ( IHost host )
151151 {
152- using var scope = webHost . Services . GetService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
152+ using var scope = host . Services . GetService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
153153 BaseDbContext dbContext = null ;
154154 try
155155 {
@@ -182,9 +182,9 @@ public static async void LoadNavigationMenuEnabledPlugins(IWebHost webHost)
182182 }
183183 }
184184
185- public static void MigrateDb ( IWebHost webHost )
185+ public static void MigrateDb ( IHost host )
186186 {
187- using var scope = webHost . Services . GetService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
187+ using var scope = host . Services . GetService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
188188 BaseDbContext dbContext = null ;
189189 try
190190 {
@@ -274,9 +274,9 @@ public static void MigrateDb(IWebHost webHost)
274274 }
275275 }
276276
277- private static async Task InitializeSettings ( IWebHost webHost , string [ ] args )
277+ private static async Task InitializeSettings ( IHost host , string [ ] args )
278278 {
279- using var scope = webHost . Services . GetService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
279+ using var scope = host . Services . GetService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
280280 var settingsService = scope . ServiceProvider . GetRequiredService < ISettingsService > ( ) ;
281281 var existsResult = settingsService . ConnectionStringExist ( ) ;
282282 if ( ! existsResult . Success ) // do need to initialize database
@@ -407,7 +407,7 @@ await SeedAdminHelper.SeedAdmin(adminSetupModel,
407407 }
408408 }
409409
410- private static IWebHost BuildWebHost ( string [ ] args )
410+ private static IHost BuildWebHost ( string [ ] args )
411411 {
412412 Console . WriteLine ( "BuildWebHost" ) ;
413413 // print all args
@@ -480,93 +480,96 @@ private static IWebHost BuildWebHost(string[] args)
480480 Environment . SetEnvironmentVariable ( "PROJECT_ID" , projectId ) ;
481481 Console . WriteLine ( "PROJECT_ID: " + projectId ) ;
482482
483- return WebHost . CreateDefaultBuilder ( args )
484- . ConfigureKestrel ( serverOptions =>
483+ return Host . CreateDefaultBuilder ( args )
484+ . ConfigureWebHostDefaults ( webBuilder =>
485485 {
486- serverOptions . Limits . MaxRequestBodySize = 100 * 1024 * 1024 ; // 100Mb
487- } )
488- . UseUrls ( $ "http://0.0.0.0:{ port } ")
489- // .UseIISIntegration()
490- . ConfigureAppConfiguration ( ( hostContext , config ) =>
491- {
492- Log . LogEvent ( "Delete all default configuration providers" ) ;
493- // delete all default configuration providers
494- config . Sources . Clear ( ) ;
495- config . SetBasePath ( hostContext . HostingEnvironment . ContentRootPath ) ;
496-
497- var filePath = Path . Combine ( hostContext . HostingEnvironment . ContentRootPath ,
498- "connection.json" ) ;
499-
500- if ( ! File . Exists ( filePath ) )
486+ webBuilder . ConfigureKestrel ( serverOptions =>
501487 {
502- ConnectionStringManager . CreateDefault ( filePath ) ;
503- }
504-
505- if ( ! string . IsNullOrEmpty ( connectionString ) )
488+ serverOptions . Limits . MaxRequestBodySize = 100 * 1024 * 1024 ; // 100Mb
489+ } )
490+ . UseUrls ( $ "http://0.0.0.0:{ port } ")
491+ // .UseIISIntegration()
492+ . ConfigureAppConfiguration ( ( hostContext , config ) =>
506493 {
507- Log . LogEvent ( $ "Creating ConnectionString file with the ConnectionString: { connectionString } ") ;
508- ConnectionStringManager . CreateWithConnectionString ( filePath , connectionString ) ;
509- }
494+ Log . LogEvent ( "Delete all default configuration providers" ) ;
495+ // delete all default configuration providers
496+ config . Sources . Clear ( ) ;
497+ config . SetBasePath ( hostContext . HostingEnvironment . ContentRootPath ) ;
510498
511- config . AddJsonFile ( "connection.json" , optional : true , reloadOnChange : true ) ;
512- var mainSettings = ConnectionStringManager . Read ( filePath ) ;
513- _defaultConnectionString = mainSettings ? . ConnectionStrings ? . DefaultConnection ;
514- config . AddEfConfiguration ( _defaultConnectionString ) ;
515- EnabledPlugins = PluginHelper . GetPlugins ( _defaultConnectionString ) ;
516- DisabledPlugins = PluginHelper . GetDisablePlugins ( _defaultConnectionString ) ;
499+ var filePath = Path . Combine ( hostContext . HostingEnvironment . ContentRootPath ,
500+ "connection.json" ) ;
517501
518- var contextFactory = new BaseDbContextFactory ( ) ;
519- if ( _defaultConnectionString != "..." )
520- {
521- string pattern = @"Database=(\d+)_Angular;" ;
522- Match match = Regex . Match ( _defaultConnectionString ! , pattern ) ;
502+ if ( ! File . Exists ( filePath ) )
503+ {
504+ ConnectionStringManager . CreateDefault ( filePath ) ;
505+ }
523506
524- if ( match . Success )
507+ if ( ! string . IsNullOrEmpty ( connectionString ) )
525508 {
526- string numberString = match . Groups [ 1 ] . Value ;
527- int number = int . Parse ( numberString ) ;
528- var disableSentry = Environment . GetEnvironmentVariable ( "DISABLE_SENTRY" ) ;
529- var sentryDisabled = ! string . IsNullOrEmpty ( disableSentry ) &&
530- ( disableSentry . ToLower ( ) == "true" || disableSentry == "1" ) ;
531- if ( ! sentryDisabled )
532- {
533- SentrySdk . ConfigureScope ( scope =>
534- {
535- scope . SetTag ( "customerNo" , number . ToString ( ) ) ;
536- Console . WriteLine ( "customerNo: " + number ) ;
537- scope . SetTag ( "osVersion" , Environment . OSVersion . ToString ( ) ) ;
538- Console . WriteLine ( "osVersion: " + Environment . OSVersion ) ;
539- scope . SetTag ( "osArchitecture" , RuntimeInformation . OSArchitecture . ToString ( ) ) ;
540- Console . WriteLine ( "osArchitecture: " + RuntimeInformation . OSArchitecture ) ;
541- scope . SetTag ( "osName" , RuntimeInformation . OSDescription ) ;
542- Console . WriteLine ( "osName: " + RuntimeInformation . OSDescription ) ;
543- } ) ;
544- }
509+ Log . LogEvent ( $ "Creating ConnectionString file with the ConnectionString: { connectionString } ") ;
510+ ConnectionStringManager . CreateWithConnectionString ( filePath , connectionString ) ;
545511 }
546512
547- using var dbContext = contextFactory . CreateDbContext ( [ _defaultConnectionString ] ) ;
548- foreach ( var plugin in EnabledPlugins )
513+ config . AddJsonFile ( "connection.json" , optional : true , reloadOnChange : true ) ;
514+ var mainSettings = ConnectionStringManager . Read ( filePath ) ;
515+ _defaultConnectionString = mainSettings ? . ConnectionStrings ? . DefaultConnection ;
516+ config . AddEfConfiguration ( _defaultConnectionString ) ;
517+ EnabledPlugins = PluginHelper . GetPlugins ( _defaultConnectionString ) ;
518+ DisabledPlugins = PluginHelper . GetDisablePlugins ( _defaultConnectionString ) ;
519+
520+ var contextFactory = new BaseDbContextFactory ( ) ;
521+ if ( _defaultConnectionString != "..." )
549522 {
550- var pluginEntity = dbContext . EformPlugins
551- . FirstOrDefault ( x => x . PluginId == plugin . PluginId ) ;
523+ string pattern = @"Database=(\d+)_Angular;" ;
524+ Match match = Regex . Match ( _defaultConnectionString ! , pattern ) ;
552525
553- if ( pluginEntity != null && ! string . IsNullOrEmpty ( pluginEntity . ConnectionString ) )
526+ if ( match . Success )
554527 {
555- if ( _defaultConnectionString . Contains ( "127.0.0.1" ) )
528+ string numberString = match . Groups [ 1 ] . Value ;
529+ int number = int . Parse ( numberString ) ;
530+ var disableSentry = Environment . GetEnvironmentVariable ( "DISABLE_SENTRY" ) ;
531+ var sentryDisabled = ! string . IsNullOrEmpty ( disableSentry ) &&
532+ ( disableSentry . ToLower ( ) == "true" || disableSentry == "1" ) ;
533+ if ( ! sentryDisabled )
556534 {
557- pluginEntity . ConnectionString = pluginEntity . ConnectionString . Replace (
558- "mariadb-cluster-mariadb-galera" ,
559- "127.0.0.1" ) ;
535+ SentrySdk . ConfigureScope ( scope =>
536+ {
537+ scope . SetTag ( "customerNo" , number . ToString ( ) ) ;
538+ Console . WriteLine ( "customerNo: " + number ) ;
539+ scope . SetTag ( "osVersion" , Environment . OSVersion . ToString ( ) ) ;
540+ Console . WriteLine ( "osVersion: " + Environment . OSVersion ) ;
541+ scope . SetTag ( "osArchitecture" , RuntimeInformation . OSArchitecture . ToString ( ) ) ;
542+ Console . WriteLine ( "osArchitecture: " + RuntimeInformation . OSArchitecture ) ;
543+ scope . SetTag ( "osName" , RuntimeInformation . OSDescription ) ;
544+ Console . WriteLine ( "osName: " + RuntimeInformation . OSDescription ) ;
545+ } ) ;
560546 }
547+ }
561548
562- plugin . AddPluginConfig ( config , pluginEntity . ConnectionString ) ;
549+ using var dbContext = contextFactory . CreateDbContext ( [ _defaultConnectionString ] ) ;
550+ foreach ( var plugin in EnabledPlugins )
551+ {
552+ var pluginEntity = dbContext . EformPlugins
553+ . FirstOrDefault ( x => x . PluginId == plugin . PluginId ) ;
554+
555+ if ( pluginEntity != null && ! string . IsNullOrEmpty ( pluginEntity . ConnectionString ) )
556+ {
557+ if ( _defaultConnectionString . Contains ( "127.0.0.1" ) )
558+ {
559+ pluginEntity . ConnectionString = pluginEntity . ConnectionString . Replace (
560+ "mariadb-cluster-mariadb-galera" ,
561+ "127.0.0.1" ) ;
562+ }
563+
564+ plugin . AddPluginConfig ( config , pluginEntity . ConnectionString ) ;
565+ }
563566 }
564567 }
565- }
566568
567- config . AddEnvironmentVariables ( ) ;
569+ config . AddEnvironmentVariables ( ) ;
570+ } )
571+ . UseStartup < Startup > ( ) ;
568572 } )
569- . UseStartup < Startup > ( )
570573 . Build ( ) ;
571574 }
572575}
0 commit comments