0

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?

2
  • can't you do WarmUp in the service1 constructor?
    – jjj
    Commented Aug 16, 2018 at 9:23
  • No, my ctors are absent of any logic (it only used for easy DI) because some of that logic is pretty heavy and too ugly for ctor like DB load or async http data request. You wont be doing this stuff in ctor but it needs to be done during startup. Since quite a few services require it I decided to make it a pattern and use a warmup for some services instead of having a mess where one half is in ctor and another is in separate method and then you have headache reading it.
    – KreonZZ
    Commented Aug 16, 2018 at 9:27

1 Answer 1

0

You can register multiple implementations to a single service type - see here. So, you could use reflection to find all the types implementing IWarmUp, then loop through them and register each type to that service type.

foreach( var warmUpType in myListOfTypesImplementingIWarmUp )
    serviceCollection.AddTransient( typeof(IWarmUp), warmUpType )

And then in InitAsync ask your IServiceProvider for an IEnumerable<IWarmUp> and loop through calling WarmUp on each object.

You can still register each type against itself like AddSingleton<Service2>() and then request it from DI as you're doing currently.

3
  • .AddTransient -- "Transient components are created every time they are requested and are never shared." Sorry I must be missing something - if all my services work as a singleton (one instance) - how would it make sense to make transient instance of each if singleton ones that I inject are different instances? I need to warm up the existing services since they are intended to be permanent through app life.
    – KreonZZ
    Commented Aug 16, 2018 at 9:43
  • Ah. Right. Well then you can just create the instances where you build up your ServiceCollection and call a different overload of AddSingleton -> AddSingleton(sp => mySingletonOne). Then do the same for each as in my answer above where myListOfTypesImplementingIWarmUp is created like new List<IWarmUp>{ mySingletonOne, mySingletonTwo }. Commented Aug 16, 2018 at 9:46
  • Think you can edit your answer to reflect what do you mean in some detail?
    – KreonZZ
    Commented Aug 16, 2018 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.