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:
parent
983078de02
commit
6e6e5ee9f2
7 changed files with 100 additions and 7 deletions
|
@ -39,9 +39,9 @@ namespace API.Services
|
|||
_cleanupService = cleanupService;
|
||||
_directoryService = directoryService;
|
||||
|
||||
|
||||
//Hangfire.RecurringJob.RemoveIfExists();
|
||||
ScheduleTasks();
|
||||
//JobStorage.Current.GetMonitoringApi().
|
||||
//JobStorage.Current.GetMonitoringApi().EnqueuedJobs()
|
||||
|
||||
}
|
||||
|
||||
|
|
44
API/Services/WarmupServiceStartupTask.cs
Normal file
44
API/Services/WarmupServiceStartupTask.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue