问题
I am also getting the error below. Is this because its not a List, and if so how do I correct this?
container.RegisterCollection<IPaymentMethod>(new[]
{
typeof(AuthorizeNetProvider),
typeof(StripeProvider),
typeof(PayPalProProvider),
typeof(PayPalStandardProvider),
typeof(IntuitProvider),
typeof(UsaEpayProvider),
typeof(ITransactProvider),
typeof(SecureNetProvider),
typeof(ExposurePayProvider),
typeof(PayTraceProvider),
typeof(BraintreeProvider)
});
Error
The configuration is invalid. Creating the instance for type IDivisionsService failed. The constructor of type PaymentManager contains the parameter with name 'paymentMethods' and type List<IPaymentMethod> that is not registered. Please ensure List<IPaymentMethod> is registered, or change the constructor of PaymentManager.
Contructor
public PaymentManager(List<IPaymentMethod> paymentMethods)
{
_paymentMethods = paymentMethods;
}
回答1:
List<T> is currently not supported as collection type. Change the constructor to one of the following:
IEnumerable<T>IList<T>ICollection<T>IReadOnlyList<T>IReadOnlyCollection<T>- T[]
The first 5 types have the following behavior:
- They behave as streams, meaning that they resolve instances from the container, every time they are iterated.
- Because of this, they are injected themselves as singletons. Their instances, however, are still resolved according to their appropriate lifestyle.
- They are immutable. Trying to add, change or remove instances from
IList<T>andICollection<T>will fail with an exception.
The last type, the array, always represent a copy of instances and will not behave as a stream. Because it is a mutable list of instances, array is always injected as Transient, even though its list of dependencies all represent Singleton instances.
来源:https://stackoverflow.com/questions/50166383/the-constructor-of-type-paymentmanager-contains-the-parameter-with-name-payment