BenCr
BenCr

Reputation: 6052

Unity ResolvedArrayParameter not injecting array of dependenceis

I've got the following constructor on one of my classes..

public JobsController(IUnitOfWork unitOfWork, IUpdateHandler<EF.Job>[] notificationHandler)

and the following registrations for my controller...

Container.RegisterType<IUpdateHandler<EF.Job>, JobNotificationHandler>();
Container.RegisterType<IUpdateHandler<EF.JobWork>, JobWorkNotificationHandler>();
Container.RegisterType<IUpdateHandler<EF.Job>, ClientSystemUpdateHandler>();

Container.RegisterType<API.JobsController>(
            new ContainerControlledLifetimeManager(),
            new InjectionConstructor(new ResolvedParameter<IUnitOfWork>(), new ResolvedArrayParameter<IUpdateHandler<EF.Job>>()));

My unitOfWork is resolved correctly but the notificationHandler parameter is always an empty array.

Do I need to register my IUpdateHandler differently if they are going to be injected as an array or am I not doing the ResolvedArrayParameter correctly in the InjectionConstructor parameter?

Upvotes: 2

Views: 716

Answers (1)

BenCr
BenCr

Reputation: 6052

I got an answer on the Unity forum.

For array resolution Unity only returns named registrations. So, I need to register IUpdateHandler with a name like so...

Container.RegisterType<IUpdateHandler<EF.Job>, JobNotificationHandler>("EFJob_Array1");

I also needed to use the ResolvedParameter instead of ResolvedArrayParameter in the InjectionConstructor.

Container.RegisterType<API.JobsController>(
    new ContainerControlledLifetimeManager(),
    new InjectionConstructor(
        new ResolvedParameter<IUnitOfWork>(), 
        new ResolvedParameter<IUpdateHandler<EF.Job>[]>()));

Upvotes: 4

Related Questions