OptionsMonitorExtensions.OnChange vs. non-default options name #110950
Unanswered
KalleOlaviNiemitalo
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Several Microsoft.Extensions.* libraries use the OptionsMonitorExtensions.OnChange<TOptions> method to register an Action<TOptions> delegate that applies the new options. This delegate does not get the name of the options as a parameter, unlike the Action<TOptions, string?> that can be registered via IOptionsMonitor<TOptions>.OnChange.
This then causes a potential bug: if an IOptionsChangeTokenSource<TOptions> implementation has been registered for a name other than Options.DefaultName, and it reports a change via its IChangeToken, then IOptionsMonitor<TOptions> gets the TOptions instance that corresponds to the non-default name and passes it to the delegates that apply these non-default options rather than IOptionsMonitor<TOptions>.CurrentValue.
I am posting this as a discussion rather than an issue, because I have not found a scenario in which an application developer would want to use non-default option names with a TOptions type that Microsoft.Extensions.* libraries monitor in this risky manner. Regardless, it seems to me that OptionsMonitorExtensions.OnChange<TOptions> is best avoided.
Here is sample code that demonstrates the bug.
Source code
CustomOptionsNameDemo.csproj
Program.cs
Actual output
Expected output
Beta Was this translation helpful? Give feedback.
All reactions