Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named Options inherit values from parent? #112365

Open
matthiaslischka opened this issue Feb 10, 2025 · 4 comments
Open

Named Options inherit values from parent? #112365

matthiaslischka opened this issue Feb 10, 2025 · 4 comments

Comments

@matthiaslischka
Copy link

Description

Not sure if that is a bug or just not expected by me, but:
Nested options that I resolve by name have values from the parent set instead of nulls.

Reproduction Steps

Given appsettings like

{
    "MySettings": {
        "SomeValue": "foo1",
        "AnotherValue": "bar1",

        "AnotherMySettings": {
            "SomeValue": "foo2"
        }
    }
}

Register both configs - default and named:

.Configure<MySettings>(null, GetConfiguration().GetSection("MySettings"))
.Configure<MySettings>("AnotherMySettings", GetConfiguration().GetSection("MySettings:AnotherMySettings"))

Resolve AnotherMySettings:

public MyService(IOptionsSnapshot<MySettings> mySettings)
{
    _mySettings = mySettings.Get("AnotherMySettings");
}

Expected behavior

_mySettings.SomeValue = foo2
_mySettings.AnotherValue = null

Actual behavior

_mySettings.SomeValue = foo2
_mySettings.AnotherValue = bar1

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

Demo Project: https://github.com/matthiaslischka/NamedOptions/

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Feb 10, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-configuration
See info in area-owners.md if you want to be subscribed.

@KalleOlaviNiemitalo
Copy link

Related comment in the source code:

public virtual void Configure(string? name, TOptions options)
{
ThrowHelper.ThrowIfNull(options);
// Null name is used to configure all named options.
if (Name == null || name == Name)
{
Action?.Invoke(options, Dependency);
}
}

If you want to configure only the default options, I think you'll need to use Options.DefaultName rather than null; or omit the argument altogether.

However, if this is by design, then it seems worth mentioning in the OptionsConfigurationServiceCollectionExtensions.Configure documentation.

@KalleOlaviNiemitalo
Copy link

The behavior looks inconsistent in that, after the .Configure<MySettings>(null, GetConfiguration().GetSection("MySettings")) registration, IOptionsChangeTokenSource<TOptions>.Name will be Options.DefaultName rather than null, even though MySettings options with all names should be reloaded.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the Future milestone Feb 11, 2025
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants