Reputation: 2277
1. Case:
Registrations:
var builder = new ContainerBuilder();
builder.RegisterType<ExportSchedule>()
.As<ISchedule>()
.InstancePerLifetimeScope();
builder.RegisterType<ImportReferenceDataSchedule>()
.As<ISchedule>()
.InstancePerLifetimeScope();
Resolving for BaseconfigScheduleHandler class:
public BaseConfigScheduleHandler(List<ISchedule> schedules) {
this.Schedules = schedules;
}
public List<ISchedule> Schedules { get; }
This case works fine. I got both registered instances of ISchedule.
2. Case
I change the registrations to only register 1 element:
var builder = new ContainerBuilder();
builder.RegisterType<ExportSchedule>()
.As<ISchedule>()
.InstancePerLifetimeScope();
In this case the dependency resolution fails with following exception:
Autofac.Core.DependencyResolutionException : An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = BaseConfigScheduleHandler (ReflectionActivator), Services = [docXtrans.TuevApi.Core.Data.Service.IScheduleHandler], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'docXtrans.TuevApi.Core.Data.Service.BaseConfigScheduleHandler' can be invoked with the available services and parameters: Cannot resolve parameter 'System.Collections.Generic.List
1[ISchedule] schedules' of constructor 'Void .ctor(System.Collections.Generic.List
1[BaseConfigSchedule], System.Collections.Generic.List`1[ISchedule])'. (See inner exception for details.)
I need both cases and want to be flexible having 1 or more elements in this collection. How can I solve that problem?
Upvotes: 0
Views: 191