1- using Gordon360 . Static_Classes ;
1+ using Gordon360 . Models . ViewModels ;
2+ using Gordon360 . Static_Classes ;
23using Microsoft . Extensions . Caching . Memory ;
4+ using Microsoft . Extensions . DependencyInjection ;
35using Microsoft . Extensions . Hosting ;
4- using System . Threading . Tasks ;
5- using System . Threading ;
66using System ;
7- using System . Diagnostics ;
8- using Gordon360 . Models . ViewModels ;
97using System . Collections . Generic ;
8+ using System . Diagnostics ;
9+ using System . Threading ;
10+ using System . Threading . Tasks ;
1011
1112namespace Gordon360 . Services ;
1213
13- public sealed class EventCacheRefreshService ( IMemoryCache cache ) : IHostedService , IDisposable
14+ public sealed class EventCacheRefreshService ( IServiceScopeFactory scopeFactory ) : IHostedService , IDisposable
1415{
1516 private Timer ? _timer = null ;
17+ private readonly IServiceScopeFactory _scopeFactory = scopeFactory ;
1618
1719 public Task StartAsync ( CancellationToken stoppingToken )
1820 {
@@ -24,18 +26,24 @@ public Task StartAsync(CancellationToken stoppingToken)
2426
2527 private async void UpdateEventsCacheAsync ( object ? state )
2628 {
27- IEnumerable < EventViewModel > ? events = null ;
2829 try
2930 {
30- events = await EventService . FetchEventsAsync ( ) ;
31+ using var scope = _scopeFactory . CreateScope ( ) ;
32+
33+ var eventService = scope . ServiceProvider . GetRequiredService < IEventService > ( ) ;
34+ var cache = scope . ServiceProvider . GetRequiredService < IMemoryCache > ( ) ;
35+
36+ var events = await eventService . FetchEventsAsync ( ) ;
37+ cache . Set ( CacheKeys . Events , events ) ;
38+
3139 }
3240 catch ( Exception ex )
3341 {
34- Debug . WriteLine ( ex . Message ) ;
42+ Debug . WriteLine ( "UpdateEventsCacheAsync error: " + ex . Message ) ;
3543 }
36- cache . Set ( CacheKeys . Events , events ) ;
3744 }
3845
46+
3947 public Task StopAsync ( CancellationToken stoppingToken )
4048 {
4149 _timer ? . Change ( Timeout . Infinite , 0 ) ;
0 commit comments