Implemented a startup task that will instantiate all the DI so first API isn't having to eat that overhead.

This commit is contained in:
Joseph Milazzo 2021-03-13 17:44:29 -06:00
parent 983078de02
commit 6e6e5ee9f2
7 changed files with 100 additions and 7 deletions

View file

@ -39,9 +39,9 @@ namespace API.Services
_cleanupService = cleanupService;
_directoryService = directoryService;
//Hangfire.RecurringJob.RemoveIfExists();
ScheduleTasks();
//JobStorage.Current.GetMonitoringApi().
//JobStorage.Current.GetMonitoringApi().EnqueuedJobs()
}

View file

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using API.Interfaces.Services;
using Microsoft.Extensions.DependencyInjection;
namespace API.Services
{
public class WarmupServicesStartupTask : IStartupTask
{
private readonly IServiceCollection _services;
private readonly IServiceProvider _provider;
public WarmupServicesStartupTask(IServiceCollection services, IServiceProvider provider)
{
_services = services;
_provider = provider;
}
public Task ExecuteAsync(CancellationToken cancellationToken)
{
using (var scope = _provider.CreateScope())
{
foreach (var singleton in GetServices(_services))
{
scope.ServiceProvider.GetServices(singleton);
}
}
return Task.CompletedTask;
}
static IEnumerable<Type> GetServices(IServiceCollection services)
{
return services
.Where(descriptor => descriptor.ImplementationType != typeof(WarmupServicesStartupTask))
.Where(descriptor => !descriptor.ServiceType.ContainsGenericParameters)
.Select(descriptor => descriptor.ServiceType)
.Distinct();
}
}
}