I'm using a lib that is based around standard MS DI stuff (which I see for the first time and not familiar with). So far it wasnt anything complicated, I build Service Provider:
var services = new ServiceCollection()
.AddSingleton<StartupService>()
.AddSingleton<Service2>()
.AddSingleton<Service3>()
.AddSingleton<Service4>()
.BuildServiceProvider();
Then I start the app via StartupService
await services.GetService<StartupService>().InitAsync();
And I inject services via ctor
in all the modules or other services that dependent on these:
private readonly Service1 service1;
private readonly Service2 service2;
public OtherService(Service1 s1, Service2 s2)
{
service1 = s1;
service2 = s2;
}
It is nothing special so far but as number of services grew and some of them required "warm up" my StartupService started to looked grim:
public async Task InitAsync()
{
Service1.WarmUp();
Service2.WarmUp();
Service3.WarmUp();
...
}
So my ~~brilliant~~ dumb idea was to implement IWarmUp
interface and just, like, iterate over service collection man:
foreach (var service in services.GetServices<IWarmUp>())
service.WarmUp();
Obviously it didnt work.
Is there a simple solution to that without making some complicated system or rewriting all my existing ctors
that already use injection? How can I get the services that implement specific interface so I can make them do contract job?
warmup
for some services instead of having a mess where one half is inctor
and another is in separate method and then you have headache reading it.