I have i case where I need to get the type implementors registered in container to use them in BackgroundService.
public abstract class ServiceRunner
{
public abstract Task RunAsync(CancellationToken token);
}
public class ServiceRunner<TService> : ServiceRunner
where TService : IService
{
ServiceRunner(/*ctor parameters*/)
{
...
}
//class logic implementation
public override async Task RunAsync(CancellationToken token)
{
...
}
}
So I'd like to get the ServiceRunner implementors, registered in container to run them all in BackgroundService worker.
public class Worker : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
public ScpWorker(IServiceProvider provider)
{
_serviceProvider = provider;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var services = _serviceProvider.GetServiceImplementors<ServiceRunner>(); //Have no idea how to implement this method
var tasks = services.Select(s => s.RunAsync(stoppingToken));
return Task.WhenAll(tasks);
}
}
Any ideas on how I can accomplish this?
Thanks in advance for your help.
From my point of view, you can get all your objects in the container
so you can use them