0
我建立Autofac容器如下Autofac配置 - 如何轉換代碼段Autofac配置
_container = ConfigureContainer(new ContainerBuilder()).Build();
我ConfigureContainer方法建立/配置Autofac ContainerBuilder如下
private static ContainerBuilder ConfigureContainer(ContainerBuilder cb)
{
cb.RegisterModule(new QuartzAutofacFactoryModule());
cb.RegisterModule(new QuartzAutofacJobsModule(Assembly.GetExecutingAssembly()));
cb.Register(l => Logging.Logger.Instance()).As<ILogger>();
var reader = new ConfigurationSettingsReader();
cb.RegisterModule(reader);
// How do I convert following lines to Autofac Config?
cb.RegisterCollection<StandardTask>("IList<StandardTask>").As<IList<StandardTask>>();
cb.RegisterType<HealthMonitoringMessageDispatcherTask>().Named<StandardTask>("HealthMonitoringTask1").MemberOf("IList<StandardTask>");
cb.RegisterType<HealthMonitoringMessageDispatcherTask>().Named<StandardTask>("HealthMonitoringTask2").MemberOf("IList<StandardTask>");
cb.RegisterType<PurgeMessageDispatcherTask>().Named<StandardTask>("PurgeTask1");
cb.RegisterType<PurgeMessageDispatcherTask>().Named<StandardTask>("PurgeTask2");
return cb;
}
我有Autofac配置部分如下
<autofac>
<components>
<!--<component type="NAB.Custom.Logging.Logger, NAB.Custom.Logging" service="NAB.Logging.Core.ILogger, NAB.Logging" />-->
<component type="NAB.Windows.ServicesConsole.Services.SchedulerService, NAB.Windows.ServicesConsole" service="NAB.Windows.ServicesConsole.Services.Core.ITopshelfService, NAB.Windows.ServicesConsole" />
<component type="NAB.Windows.ServicesConsole.Jobs.HealthMonitoringMessageDispatcherJob, NAB.Windows.ServicesConsole" />
<component type="NAB.Windows.ServicesConsole.Jobs.PurgeMessageDispatcherJob, NAB.Windows.ServicesConsole" />
</components>
</autofac>
它解決了配置的com Ponent(波納恩特)從自定義配置節正確,但是,我想移動從下面的代碼自定義配置節登記爲好,在那裏我無法找到...
// How do I convert following lines to Autofac Config?
cb.RegisterCollection<StandardTask>("IList<StandardTask>").As<IList<StandardTask>>();
cb.RegisterType<HealthMonitoringMessageDispatcherTask>().Named<StandardTask>("HealthMonitoringTask1").MemberOf("IList<StandardTask>");
cb.RegisterType<HealthMonitoringMessageDispatcherTask>().Named<StandardTask>("HealthMonitoringTask2").MemberOf("IList<StandardTask>");
cb.RegisterType<PurgeMessageDispatcherTask>().Named<StandardTask>("PurgeTask1");
cb.RegisterType<PurgeMessageDispatcherTask>().Named<StandardTask>("PurgeTask2");
任何建議妥善的解決辦法?小代碼片段將非常有用。基本上我註冊名爲集合,然後將組件注入集合,這是我的註冊對象之一的構造參數。
* Autofac *你使用的版本是? 'RegisterCollection'自從版本2開始被棄用.' IEnumerable'總是可以解析的,並且將包含服務'T'的所有註冊組件。以下文檔可幫助您瞭解如何使用autofac收集http://docs.autofac.org/en/latest/resolve/relationships.html#enumeration-ienumerable-b-ilist-b-icollection-b –