using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace Selector.Web.Service { public class CacheEventProxyService: IHostedService { private readonly ILogger Logger; private readonly IServiceScopeFactory ScopeFactory; private readonly IEnumerable CacheEvents; public CacheEventProxyService( ILogger logger, IEnumerable mappings, IServiceScopeFactory scopeFactory ) { Logger = logger; ScopeFactory = scopeFactory; CacheEvents = mappings; } public Task StartAsync(CancellationToken cancellationToken) { Logger.LogInformation("Starting cache event proxy"); foreach (var mapping in CacheEvents) { mapping.ConstructMapping(); } using (var scope = ScopeFactory.CreateScope()) { var hubProxy = scope.ServiceProvider.GetRequiredService(); hubProxy.FormMappings(); } return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { Logger.LogInformation("Stopping cache hub proxy"); return Task.CompletedTask; } } }