问题
I'm trying to register a collection along with its instanceCreator. I wasn't able to find any overload for the Container.Collection.Register
method which accepts an instanceCreator. If i try to run a loop and register a type along with its instanceCreator multiple times using the Container.Register(instanceCreator, lifestyle)
, i'm unable to do it because i get the error that type cannot be registerd multiple times.
Basically what i'm trying to do is:
foreach(var item in items){
Container.Register<ILogWriter>(instanceCreator, lifestyle)
}
I have multiple implementations of ILogWriter
and each have their own different configurations which need to passed in the constructor, hence i need to use the instanceCreator overload.
TIA
回答1:
Container.Collection.Register
is meant for registering the whole collection at once. For instance by using batch-registration and supplying a list of Assembly
instances, or a list of Type
instances.
What you are looking for, on the other hand, is registering a single item for a collection of instances. This is done using the Container.Collection.Append
overloads:
container.Collection.Append<ILogWriter>(instanceCreator, lifestyle);
来源:https://stackoverflow.com/questions/60781095/simpleinjector-register-collection-with-instancecreator