Search code examples
configautofacnamedconfigurationsection

Autofac Configuration - How do I convert Code Section to Autofac Config


I am building Autofac Container as below

_container = ConfigureContainer(new ContainerBuilder()).Build();

I have ConfigureContainer method to build/configure Autofac ContainerBuilder as below

        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;
        }

I have Autofac configuration section as below

<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>

It resolves the configured component from the Custom Configuration Section correctly, but, I want to move following registrations from code to Custom Configuration Section as well, where I am unable to find proper solution for...

            // 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");

Any suggestions? Little code snippet will be very useful. Basically I am registering named collection and then injecting components to the collection which is constructor parameter of one of my registered objects.


Solution

  • Documentation on how configuration works in Autofac is here. Unless you're on the 4.0 beta, you're probably looking at the 3.x XML configuration style.

    You figured out component registrations. To add a key/name to a registration, just add that attribute.

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

    There is no XML configuration support for named collections. If you're stuck using them and can't move to just IEnumerable<T> then it's best if you just leave those in a module and register the module via configuration.

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