API Proposal: Add BindConfiguration extension method for OptionsBuilder #34191
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR has been copied from dotnet/extensions#3020
Summary
BindConfigurationextension method forOptionsBuilder<TOptions>.Description
There are situations in which the
IConfigurationinstance from which to configure options has not yet been materialized when a method likeConfigureServicesis being executed. For example, the .NET Core Generic Host Builder offers theConfigureServicesmethod which does not offer access to the Application Configuration instance that will materialize when the Host is built. (Note: A fully materialized host configuration instance can be obtained from theHostBuilderContextinstance that is passed to the callback, however, this host configuration can differ from the Application configuration and may not offer the desired behaviour.)In such situations, the
Bindextension methods on theOptionsBuilder<TOptions>cannot be called, since they all require a fully materializedIConfigurationinstance. Instead, you need to call theConfigureextension method, specifyingIConfigurationas a DI dependency for configuration and then call theIConfiguration.Bindextension method in the specifying configuration callback action.Since binding from a DI-provided
IConfigurationis a fairly usual scenario when using the .NET Generic Host (i.e. without using a Startup class), it would be advantageous to simplify this scenario by providing 1st class support in form of an extension method that performs the necessary steps.Proposal
BindConfigurationextension method forOptionsBuilder<TOptions>.Usage
Previous implementation
The proposed API is a convenience wrapper around existing APIs. The code above could previously be implemented as follows:
Comments
The name
BindConfigurationwas chosen, since the arguments of the extension method do not directly conway how binding is performed. The existingBindextension methods all take anIConfigurationinstance as argument, and in these cases the binding is implied from the argument type. Conceptually, the proposed API can be thought of as an overload to the existingBindextension methods.