@@ -11,6 +11,7 @@ namespace RP1AnalyticsWebApp.Services
1111 public class StartupHostedService : IHostedService
1212 {
1313 private readonly IServiceProvider _serviceProvider ;
14+
1415 public StartupHostedService ( IServiceProvider serviceProvider )
1516 {
1617 _serviceProvider = serviceProvider ;
@@ -19,24 +20,33 @@ public StartupHostedService(IServiceProvider serviceProvider)
1920 public async Task StartAsync ( CancellationToken cancellationToken )
2021 {
2122 using IServiceScope scope = _serviceProvider . CreateScope ( ) ;
22- await EnsureRoles ( scope ) ;
23+ var rolesTask = EnsureRolesAsync ( scope ) ;
24+ _ = PreloadCacheAsync ( scope , cancellationToken ) ; // Start but do not wait for completion
25+
26+ await rolesTask ;
2327 }
2428
2529 public Task StopAsync ( CancellationToken cancellationToken ) => Task . CompletedTask ;
2630
27- private static async Task EnsureRoles ( IServiceScope scope )
31+ private static async Task EnsureRolesAsync ( IServiceScope scope )
2832 {
2933 var _roleManager = scope . ServiceProvider . GetRequiredService < RoleManager < MongoRole > > ( ) ;
30- await Task . WhenAll ( EnsureRole ( _roleManager , Constants . Roles . Admin ) ,
31- EnsureRole ( _roleManager , Constants . Roles . Member ) ) ;
34+ await Task . WhenAll ( EnsureRoleAsync ( _roleManager , Constants . Roles . Admin ) ,
35+ EnsureRoleAsync ( _roleManager , Constants . Roles . Member ) ) ;
3236 }
3337
34- private static async Task EnsureRole ( RoleManager < MongoRole > _roleManager , string roleName )
38+ private static async Task EnsureRoleAsync ( RoleManager < MongoRole > _roleManager , string roleName )
3539 {
3640 if ( ! await _roleManager . RoleExistsAsync ( roleName ) )
3741 {
3842 await _roleManager . CreateAsync ( new MongoRole ( roleName ) ) ;
3943 }
4044 }
45+
46+ private static async Task PreloadCacheAsync ( IServiceScope scope , CancellationToken cancellationToken )
47+ {
48+ var cache = scope . ServiceProvider . GetRequiredService < CacheService > ( ) ;
49+ await cache . InitCacheAsync ( cancellationToken ) ;
50+ }
4151 }
4252}
0 commit comments