-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Create .Net plain options injection helpers that support the following:
AddTransientOptions<TOptions>()
AddScopedOptions<TOptions>()
AddSingletonOptions<TOptions>()
Conditional options injection should be supported.
Implementation example:
public static class OptionsBuilderExtensions
{
public static OptionsBuilder<TOptions> AddScopedOptions<TOptions>(this IServiceCollection services)
where TOptions : class
{
services.AddScoped(serviceProvider => serviceProvider
.GetService<IOptionsSnapshot<TOptions>>().Value);
return services.AddOptions<TOptions>();
}
}
Registration example:
services.AddScopedOptions<Options>()
.Configure(o => o.MyImportantProperty = "Some important value");
That should allow injection the options directly, controlling the lifetime from the composition root instead of from the consumer, like the following:
public class Consumer
{
private readonly Options _options;
public Consumer(Options options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
}
Metadata
Metadata
Assignees
Labels
No labels