Repository navigation
Description
Description
Key changes:
- By comparing normalized prefixes to normalized keys, both the un-altered and colon-normalized prefixes match environment variables in either the __ or colon formats.
- There is a change of behaviour that some environment variables which theoretically didn't match the filter before may now match the filter, but I don't expect this to be an issue in practice (I very much doubt anyone configures variables with both a
:and__prefix, and relies on the fact that these don't match) - Existing behaviour is maintained, to avoid any potential issues such as Breaking change in environment variable configuration prefix support in dotnet 6 runtime#61577 or any changes of behaviour with the special case connection string matching.
Version
.NET 7 Preview 4
Previous behavior
Some environment variables which theoretically didn't match the filter before may now match the filter
New behavior
By comparing normalized prefixes to normalized keys, both the un-altered and colon-normalized prefixes match environment variables in either the __ or colon formats.
Type of breaking change
- Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
- Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.
Reason for change
Confusing environment variable configuration prefix behaviour was reintroduced in 6.0.2. (It worked on 6.0.0 and 6.0.1. Upgrading to 6.0.2 caused the break.) dotnet/runtime#62819 was merged into 6.0.2 to revert a breaking change in 6.0.0, but the reversion caused a breaking change compared to 6.0.0/6.0.1, by reverting the (good) part of dotnet/runtime#62819.
For reference, the current configuration documentation is here.
Recommended action
Environment.SetEnvironmentVariable("MY_PREFIX__ConfigKey, "myFooValue");
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables("MY_PREFIX__")
.Build();
var loadedValue = configuration.GetValue<string?>("ConfigKey", null);
Assert.Equals("myFooValue", loadedValue); // FAILED in 6.0.2 as loadedConfig was null. The prefix of `MY_PREFIX:` can be specified instead nowFeature area
C#, Core .NET libraries, Extensions
Affected APIs
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fields✅ Done
Activity
maryamariyan commentedon Sep 29, 2022
When I was trying to create this issue the
.NET 7 Preview 4option was not available so selected.NET 7 Preview 5from the drop down list.gewarren commentedon Oct 11, 2022
@maryamariyan Does this breaking change only affect Unix-based OSs? See my comment/code here, which I would expect to return
Trueon .NET 7 RC 2.dhedey commentedon Oct 12, 2022
Hi @gewarren apologies, I'm unable reply to you on dotnet/runtime#65756 as the issue is closed, and I'm only a Contributor.
But yes, I'm surprised by this behaviour. The PR includes a test case
AddEnvironmentVariablesUsingPrefixWithDoubleUnderscores_Bind_PrefixMatches(see this line in github) which is a test very similar to your example. I'm surprised the test passes while your example fails, unless the tests don't run on Windows in CI. The matching of normalized key against normalized prefix should also be OS-independent.Your code prints
Trueon .NET 7 RC 2 on Mac for me.EDIT: I've also managed to get access to a Windows 11 PC. It also prints
Trueon Windows for me:Is there perhaps anything notable about your set-up or .NET 7 version?
gewarren commentedon Oct 12, 2022
Thanks @dhedey, I did get it to print
Trueafter updating my NuGet packages for the extensions to .NET 7 (forgot about that).