2015-09-10 98 views
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"); 

任何建議妥善的解決辦法?小代碼片段將非常有用。基本上我註冊名爲集合,然後將組件注入集合,這是我的註冊對象之一的構造參數。

+0

* Autofac *你使用的版本是? 'RegisterCollection'自從版本2開始被棄用.' IEnumerable '總是可以解析的,並且將包含服務'T'的所有註冊組件。以下文檔可幫助您瞭解如何使用autofac收集http://docs.autofac.org/en/latest/resolve/relationships.html#enumeration-ienumerable-b-ilist-b-icollection-b –

回答

1

Documentation on how configuration works in Autofac is here.除非您使用4.0測試版,否則您可能正在查看3.x XML configuration style

您計算出組件註冊。要向註冊添加一個密鑰/名稱,只需添加該屬性即可。

<component type="Service" 
      service="IService" 
      name="the-key-goes-here" /> 

對命名集合沒有XML配置支持。如果你使用它們並且不能移動到IEnumerable<T>,那麼最好將它們留在模塊中並通過配置註冊模塊。

<modules> 
    <module type="MyModule" /> 
</modules>