11using System ;
2+ using System . Runtime . InteropServices ;
3+ using Microsoft . Extensions . Configuration ;
24using Microsoft . Extensions . DependencyInjection ;
3- using Microsoft . Extensions . Logging ;
45using SampleShared ;
56using Spring . Context . Support ;
67using Spring . Extensions . DependencyInjection ;
@@ -11,30 +12,40 @@ public static class Program
1112 {
1213 public static void Main ( )
1314 {
14- var factory = new SpringServiceProviderFactory ( ContextRegistry . GetContext ( ) ) ;
15+ var factory = new SpringServiceProviderFactory ( options =>
16+ {
17+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
18+ {
19+ options . Parent = ContextRegistry . GetContext ( ) ;
20+ }
21+ else
22+ {
23+ // Mono doesn't read app.config
24+ var context = new CodeConfigApplicationContext ( ) ;
25+ context . ScanWithTypeFilter ( t => t . Name . EndsWith ( "SpringConfiguration" ) ) ;
26+ context . Refresh ( ) ;
27+ options . Parent = context ;
28+ }
29+ } ) ;
1530 var services = new ServiceCollection ( ) ;
1631 ConfigureServices ( services ) ;
17- var context = factory . CreateBuilder ( services ) ;
18- var provider = factory . CreateServiceProvider ( context ) ;
19-
20- using ( provider as IDisposable )
21- {
22- var clock = provider . GetRequiredService < ISystemClock > ( ) ;
23- Console . WriteLine ( $ "Current DateTime is { clock . Now : O} ") ;
24- }
25-
26- Console . WriteLine ( "Press any key to exit ..." ) ;
27- Console . ReadKey ( ) ;
32+ var resolver = factory . CreateServiceProvider ( factory . CreateBuilder ( services ) ) ;
33+ var clock = resolver . GetRequiredService < ISystemClock > ( ) ;
34+ Console . WriteLine ( $ "Current DateTime is { clock . Now : O} ") ;
2835 }
2936
3037 private static void ConfigureServices ( IServiceCollection services )
3138 {
32- services . AddLogging ( logging => logging . AddConsole ( ) ) ;
33- var dummyClock = Environment . GetEnvironmentVariable ( "SYSTEM_CLOCK" ) ;
34- if ( ! string . IsNullOrWhiteSpace ( dummyClock ) && DateTime . TryParse ( dummyClock , out var dummyDateTime ) )
39+ var configuration = new ConfigurationBuilder ( )
40+ . AddJsonFile ( "appsettings.json" , optional : true )
41+ . AddJsonFile ( $ "appsettings.{ Environment . GetEnvironmentVariable ( "DOTNET_ENVIRONMENT" ) ?? "Production" } .json",
42+ optional : true )
43+ . Build ( ) ;
44+ services . AddSingleton < IConfiguration > ( configuration ) ;
45+ if ( configuration [ "SystemClock" ] ? . Equals ( "Dummy" , StringComparison . OrdinalIgnoreCase ) == true )
3546 {
36- services . AddSingleton < ISystemClock > ( _ => new DummySystemClock ( dummyDateTime ) ) ;
47+ services . AddSingleton < ISystemClock , DummySystemClock > ( ) ;
3748 }
3849 }
3950 }
40- }
51+ }
0 commit comments