为了将IServiceVisitors 注入Service你只需要注册它们。
<configuration>
<autofac defaultAssembly="App">
<components>
<component type="App.Service"
service="App.IService" />
<component type="App.ServiceVisitor1"
service="App.IServiceVisitor" />
<component type="App.ServiceVisitor2"
service="App.IServiceVisitor" />
</components>
</autofac>
</configuration>
在这种情况下,您不需要指定IEnumerable<IServiceVisitor>. 如果没有注册,Autofac会自动生成一个空数组。IServiceVisitor
public Service(IRepo repo, IEnumerable<IServiceVisitor> serviceVisitors)
{ /* ... */ }
如果您不需要 aIEnumerable<IServiceVisitor>但需要一个可选项IServiceVisitor,则只需在构造函数中将其声明为可选项= null
public Service(IRepo repo, IServiceVisitor serviceVisitor = null)
{ /* ... */ }