11using Hypermc . Services ;
22using Hypermc . Settings ;
33using Hypermc . UI . Views ;
4+ using Microsoft . Extensions . Configuration ;
45using Microsoft . Extensions . DependencyInjection ;
6+ using Microsoft . Extensions . Hosting ;
57using System ;
68using System . Collections . Generic ;
9+ using System . IO ;
710using System . Linq ;
811using System . Threading . Tasks ;
912using System . Windows . Forms ;
@@ -12,26 +15,33 @@ namespace Hypermc
1215{
1316 static class Program
1417 {
15- private static IServiceProvider Provider ;
1618 /// <summary>
1719 /// The main entry point for the application.
1820 /// </summary>
1921 [ STAThread ]
2022 static async Task Main ( )
2123 {
22- Provider = ConfigureService ( ) ;
23- await Provider . GetRequiredService < IUserSettings > ( ) . Initialize ( ) ;
24+ var builder = new ConfigurationBuilder ( ) ;
25+ BuildConfig ( builder ) ;
26+ builder . Build ( ) ;
27+
28+ var host = Host . CreateDefaultBuilder ( )
29+ . ConfigureServices ( ( context , services ) =>
30+ {
31+ ConfigureServices ( services ) ;
32+ } )
33+ . Build ( ) ;
34+
35+ await host . Services . GetRequiredService < IUserSettings > ( ) . Initialize ( ) ;
2436
2537 Application . SetHighDpiMode ( HighDpiMode . SystemAware ) ;
2638 Application . EnableVisualStyles ( ) ;
2739 Application . SetCompatibleTextRenderingDefault ( false ) ;
28- Application . Run ( Provider . GetRequiredService < HyperMcView > ( ) ) ;
40+ Application . Run ( host . Services . GetRequiredService < HyperMcView > ( ) ) ;
2941 }
3042
31- private static IServiceProvider ConfigureService ( )
43+ private static IServiceProvider ConfigureServices ( IServiceCollection services )
3244 {
33- IServiceCollection services = new ServiceCollection ( ) ;
34-
3545 services . AddSingleton < HyperMcView > ( )
3646 . AddSingleton < IUserSettings , UserSettings > ( ) ;
3747
@@ -42,5 +52,13 @@ private static IServiceProvider ConfigureService()
4252
4353 return services . BuildServiceProvider ( ) ;
4454 }
55+
56+ static void BuildConfig ( IConfigurationBuilder builder )
57+ {
58+ builder . SetBasePath ( Directory . GetCurrentDirectory ( ) )
59+ . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
60+ . AddJsonFile ( $ "appsettings.{ Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ?? "Production" } .json", optional : true )
61+ . AddEnvironmentVariables ( ) ;
62+ }
4563 }
4664}
0 commit comments