Autofac 配置 - 如何将代码部分转换为 Autofac 配置
Autofac Configuration - How do I convert Code Section to Autofac Config
我正在构建 Autofac 容器,如下所示
_container = ConfigureContainer(new ContainerBuilder()).Build();
我有 ConfigureContainer 方法 build/configure 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>
它正确地解析了自定义配置部分中的配置组件,但是,我也想将以下注册从代码移动到自定义配置部分,在那里我找不到合适的解决方案...
// 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");
有什么建议吗?小代码片段将非常有用。基本上我正在注册 named 集合,然后将组件注入集合,这是我注册的对象之一的构造函数参数。
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.
您了解了组件注册。要将 key/name 添加到注册中,只需添加该属性即可。
<component type="Service"
service="IService"
name="the-key-goes-here" />
没有 XML 命名集合的配置支持。如果您一直在使用它们并且不能仅移动到 IEnumerable<T>
那么最好将它们留在模块中并通过配置注册模块。
<modules>
<module type="MyModule" />
</modules>
我正在构建 Autofac 容器,如下所示
_container = ConfigureContainer(new ContainerBuilder()).Build();
我有 ConfigureContainer 方法 build/configure 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>
它正确地解析了自定义配置部分中的配置组件,但是,我也想将以下注册从代码移动到自定义配置部分,在那里我找不到合适的解决方案...
// 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");
有什么建议吗?小代码片段将非常有用。基本上我正在注册 named 集合,然后将组件注入集合,这是我注册的对象之一的构造函数参数。
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.
您了解了组件注册。要将 key/name 添加到注册中,只需添加该属性即可。
<component type="Service"
service="IService"
name="the-key-goes-here" />
没有 XML 命名集合的配置支持。如果您一直在使用它们并且不能仅移动到 IEnumerable<T>
那么最好将它们留在模块中并通过配置注册模块。
<modules>
<module type="MyModule" />
</modules>