LightInject icon indicating copy to clipboard operation
LightInject copied to clipboard

GetAllInstances misses some of my instances

Open ift-jsy opened this issue 9 years ago • 4 comments
trafficstars

Hi,

I've got some problems with the GetAllInstances method which didn't gave me all the desired instances. Here is the context:

  • one main project with 3 assemblies (for each of them I've got a CompositionRoot and its declaration thru the Assembly attribute)
  • each assembly register an IModuleTag with a dedicated and unique service name
  • for each Assembly, I use the RegisterAssembly method
  • in the end, I try to gather all my IModuleTag instances

When I use the GetAllInstances method, I just get 1 of my instances but when I ask for them separately (and of course using their names), I can found them (below an extract of my test)

_container.GetInstance<IModuleTag>()); => Yes found _container.GetInstance<IModuleTag>("account")); => Yes found _container.GetInstance<IModuleTag>("admin")); => Yes found _container.GetInstance<IModuleTag>("import")); => Yes found _container.GetAllInstances<IModuleTag>().Count()); => 1 which is "account" _container.GetAllInstances(typeof(IModuleTag)).Count());$ => 1 which is "account"

Any idea?

EDIT: I missed to give some details:

  • LightInject : 4.0.8
  • LightInject.Annotation: 1.0.0.4

ift-jsy avatar Feb 25 '16 13:02 ift-jsy

Can you produce a simple console application with this code?

seesharper avatar Feb 26 '16 10:02 seesharper

Of course :)

This a simple solution which reproduces the bug: GetAllInstances.zip

ift-jsy avatar Feb 27 '16 08:02 ift-jsy

I think the issue is related to services being registered after the first call to GetAllInstances.

Would it be possible for you to ensure that all composition roots has been executed before you start to requesting services from the container?

seesharper avatar Mar 01 '16 11:03 seesharper

Hi,

Unfortunately, I have to register my services in "at least" 2 steps. I will try to use, the first time, only the "GetInstance" method and then the "GetAllInstances".

I took a look in the container and I found out that the "emitters" and the "availableServices" contain my registered services.

I dont know if it should be consider as a bug or an improvement, but it'll be super cool that have this scenario working.

ift-jsy avatar Mar 02 '16 08:03 ift-jsy