@@ -37,7 +37,7 @@ public partial class App
3737{
3838 private CancellationTokenSource ServerCtx { get ; set ; }
3939
40- protected override void OnStartup ( StartupEventArgs e )
40+ protected override async void OnStartup ( StartupEventArgs e )
4141 {
4242 DispatcherUnhandledException += App_DispatcherUnhandledException ;
4343 AppDomain . CurrentDomain . UnhandledException += GlobalUnhandledException ;
@@ -100,49 +100,28 @@ protected override void OnStartup(StartupEventArgs e)
100100 serviceCollection . AddSingleton < IClientRegistry < ILobbyClient > , ClientRegistry < ILobbyClient > > ( ) ;
101101 serviceCollection . AddSingleton < LobbyServer > ( ) ;
102102 serviceCollection . AddSingleton < ILobbyServer < ILobbyClient > > ( sp => sp . GetRequiredService < LobbyServer > ( ) ) ;
103- serviceCollection . AddSingleton < IHostedService > ( sp => sp . GetRequiredService < LobbyServer > ( ) ) ;
104103
105104 // Login
106105 serviceCollection . AddSingleton < IClientFactory < LoginClient > , ClientFactory < LoginClient > > ( ) ;
107106 serviceCollection . AddSingleton < IClientRegistry < ILoginClient > , ClientRegistry < ILoginClient > > ( ) ;
108107 serviceCollection . AddSingleton < LoginServer > ( ) ;
109108 serviceCollection . AddSingleton < ILoginServer < ILoginClient > > ( sp => sp . GetRequiredService < LoginServer > ( ) ) ;
110- serviceCollection . AddSingleton < IHostedService > ( sp => sp . GetRequiredService < LoginServer > ( ) ) ;
111109
112110 // World
113111 serviceCollection . AddSingleton < IClientFactory < WorldClient > , ClientFactory < WorldClient > > ( ) ;
114112 serviceCollection . AddSingleton < IClientRegistry < IWorldClient > , ClientRegistry < IWorldClient > > ( ) ;
115113 serviceCollection . AddSingleton < WorldServer > ( ) ;
116114 serviceCollection . AddSingleton < IWorldServer < IWorldClient > > ( sp => sp . GetRequiredService < WorldServer > ( ) ) ;
117- serviceCollection . AddSingleton < IHostedService > ( sp => sp . GetRequiredService < WorldServer > ( ) ) ;
115+
116+ // Hosted Services
117+ serviceCollection . AddSingleton < IHostedService , ServerOrchestrator > ( ) ;
118118
119119 var serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
120120 serviceProvider . GetService < IServer > ( ) ;
121121 var hostedServices = serviceProvider . GetServices < IHostedService > ( ) . ToArray ( ) ;
122122
123- // Start the hosted services in a dedicated long-running task
124- _ = Task . Run ( async ( ) =>
125- {
126- try
127- {
128- // Start all hosted services
129- await Task . WhenAll ( hostedServices . Select ( svc => svc . StartAsync ( ServerCtx . Token ) ) ) ;
130-
131- // Then wait until the token is cancelled
132- try
133- {
134- await Task . Delay ( Timeout . Infinite , ServerCtx . Token ) ;
135- }
136- catch ( OperationCanceledException )
137- {
138- // Expected when the token is canceled
139- }
140- }
141- catch ( Exception exception )
142- {
143- SentrySdk . CaptureException ( exception ) ;
144- }
145- } , ServerCtx . Token ) ;
123+ foreach ( var svc in hostedServices )
124+ await svc . StartAsync ( ServerCtx . Token ) ;
146125 }
147126 catch ( Exception exception )
148127 {
0 commit comments