本文https://medium.com/@dmitryzaets/legacy-net-applications-configuration-management-net-framework-4-5-1-68220335d9d8介绍了如何将选项模式与Autofac一起使用。我尝试将其翻译为与Simple Injector一起使用。但是我没有运气。 这是我的IOC代码
public class IocBootstrap2
{
private Container Container { get; }
public IocBootstrap2()
{
Container = new Container();
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Configuration"))
.AddJsonFile("settings.json", optional: false, reloadOnChange: true);
var configuration = configurationBuilder.Build();
//Register Options
Container.Register(typeof(IOptions<>), typeof(OptionsManager<>));
Container.Register(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>));
Container.Register(typeof(IOptionsFactory<>), typeof(OptionsFactory<>));
Container.Register(typeof(IOptionsMonitorCache<>), typeof(OptionsCache<>));
// Register ConfigurationOptions
Container.RegisterConfigurationOptions2<MailingOptions>(configuration.GetSection("mailing"));
#if DEBUG
Container.Verify();
#endif
}
}
public static class ConfigurationSetupExtensions2
{
public static void RegisterConfigurationOptions2<TOptions>(this Container container, IConfiguration config)
where TOptions : class
{
container.Register(typeof(IOptionsChangeTokenSource<TOptions>),
() => new ConfigurationChangeTokenSource<TOptions>(config), Lifestyle.Transient);
container.Register(typeof(IConfigureOptions<TOptions>),
() => new ConfigureFromConfigurationOptions<TOptions>(config), Lifestyle.Transient);
}
}
public class MailingOptions
{
public MailingOptions()
{
BatchSize = 1;
}
public int BatchSize { get; set; }
public int BatchDelay { get; set; }
}
settings.json
{
"mailing": {
"batchSize": 15,
"batchDelay": 1
}
}
然后将其注入到这样的ViewModel:s构造函数中
public class BlockViewModel
{
private readonly MailingOptions _options;
#region Constructor
public BlockViewModel(IOptions<MailingOptions> options)
{
_options = options.Value;
}
#endregion
}
当我运行它时,我在Container.Verify中得到了异常。
OptionsFactory
类型的构造函数包含名称为“ setups”且类型为IEnumerable >的参数,该参数未注册。请确保已注册IEnumerable >,或更改OptionsFactory 的构造函数。但是,有一个IConfigureOptions 的注册。您是否要依靠IConfigureOptions ?
StackTrace:
在SimpleInjector.Container.ThrowParameterTypeMustBeRegistered(InjectionTargetInfo目标)
我如何注册IEnumerable<IConfigureOptions<MailingOptions>>
?
有人可以告诉我我在做错什么,或更准确地说,是我不了解的是什么?
答案 0 :(得分:3)
简短的答案是:不要将IOptions<T>
注入您的应用程序组件。如here所述,这只会使您的组件,它们的单元测试以及您的配置(如您已经注意到的)复杂化。
相反,让BlockViewModel
直接依赖MailingOptions
:
public class BlockViewModel
{
private readonly MailingOptions _options;
public BlockViewModel(MailingOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
}
这使您可以将配置简化为以下内容:
Container = new Container();
var configutation = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration"))
.AddJsonFile("settings.json", optional: false);
.Build();
MailingOptions options = configuration.GetSection("mailing").Get<MailingOptions>();
Container.RegisterInstance<MailingOptions>(options);
// Register View Models
Container.Register<BlockViewModel>();
Container.Verify();