Skip to content

AddEnvironmentVariables(prefix) - prefix is not respected by generic host #61653

@legezam

Description

@legezam

Describe the bug

.AddEnvironmentVariables(string prefix) behaves inconsistently when used in .NET generic host.

When creating a plain ConfigurationBuilder and calling .AddEnvironmentVariables(prefix) or combining it with a ServiceCollection, the provided prefix value is respected. If all the above is inside a .NET Generic Host builder, the prefix somehow gets ignored. Confirmed on .NET 5, linux (fedora) and OSX.

To Reproduce

https://gist.github.com/legezam/44343abf8649b2900f7214020d82aa5e

The above code will be sufficient to reproduce the inconsistency.

Use this for appsettings.json:

{
    "Defaults": {
        "Instance": "NOT OK"
    }
}

run this from terminal:
env foo_Defaults__Instance=foo dotnet run

I expect the following output:

foo
foo
foo

Instead i get:

NOT OK
foo
foo

Exceptions (if any)

  • no

Further technical details

   dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     fedora
 OS Version:  35
 OS Platform: Linux
 RID:         fedora.35-x64
 Base Path:   /home/bela/.local/share/dotnet/sdk/6.0.100/

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  3.0.103 [/home/bela/.local/share/dotnet/sdk]
  3.1.409 [/home/bela/.local/share/dotnet/sdk]
  5.0.101 [/home/bela/.local/share/dotnet/sdk]
  5.0.200 [/home/bela/.local/share/dotnet/sdk]
  5.0.300 [/home/bela/.local/share/dotnet/sdk]
  6.0.100 [/home/bela/.local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.6 [/home/bela/.local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [/home/bela/.local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.6 [/home/bela/.local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [/home/bela/.local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

restricting the project to use 5.0.300, through global.json. No IDE is involved, running from terminal.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions