Registering instances of the same type with a simple injector

I just selected Simple Injector and all I want to do now is:

Container.Register<IMessageHandler>(() => new MessageHandlerOne());
Container.Register<IMessageHandler>(() => new MessageHandlerTwo());

      

I'm just trying to register two message handlers so I can iterate over them later.

To get those registered instances back this method sounds like it's a trick.

var instances = Container.GetAllInstances<IMessageHandler>();
//why doesn't instances contain both MessageHandlerOne, and MessageHandlerTwo ?

      

Instead, it comes back empty IEnumerable

.

What am I doing wrong? For something called Simple Injector, do you think this will work? Am I thinking this wrong?

+3


source to share


1 answer


What you are trying to do should fail with an exception on the second call Container.Register<IMessageHandler>

, as the simple injector API clearly distinguishes registering collections .

In other words, if you want to enable collection, you need to register the collection:



Container.RegisterAll<IMessageHandler>(
    typeof(MessageHandlerOne), 
    typeof(MessageHandlerTwo));

      

+4


source







All Articles