Skip to content

IServiceCollection Configure should have an overload taking Func<TOptions> #85276

Open
@jez9999

Description

@jez9999

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I'm not exactly sure why, but at the moment the only way of setting up options with a factory in ASP.NET is to take an unconfigured instance of an options object, and modify it to have the correct values. There doesn't appear to be a way to just return the options object from the factory.

Describe the solution you'd like

There should be an overload in OptionsServiceCollectionExtensions of Configure that takes a Func<TOptions> so I can just return an options set instead of having to map each value manually. I have an overarching options object I first bind, then configure subsets of that options object that may need to be pulled in. Currently I need to do this:

private readonly WebAppOptions _options = new();

[...]

_conf.Bind(_options);
services
	.Configure<WebAppOptions>(_conf);
services
	.Configure<TurnsProcessorOptions>(opts => {
		opts.EmptyRoundsForGameDraw = _options.TurnsProcessorOptions.EmptyRoundsForGameDraw;
		opts.TurnTimeoutInterval = _options.TurnsProcessorOptions.TurnTimeoutInterval;
	});

A Func<TOptions> overload would allow me to simply do:

_conf.Bind(_options);
services
	.Configure<WebAppOptions>(_conf);
services
	.Configure<TurnsProcessorOptions>(() => _options.TurnsProcessorOptions);

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions