Using AddEnvironmentVariables in .net core 3.1 app
Asked Answered
F

1

7

I have generated a new web project. it seems that in .net core 3.1 the appSettings.jsons were generated and working fine. the problem is that they are loaded and controlled by the runtime and not me. So I cant invoke AddEnvironmentVariables

Where is the right place to call AddEnvironmentVariables in such case?

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
}

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
Forgotten answered 5/2, 2020 at 16:7 Comment(6)
Did you able to see launchSetting.json file your project.?Purpure
@ArpitJain yes. BTW, the appsettings is generated and loaded, the problem is that it is loaded by the runtime and not by me, i I can really invoke: AddEnvironmentVariablesForgotten
We have to add in LaunchSetting.json file and in Startup.cs filePurpure
@ArpitJain This is how the project was generated... I dont unerstand... the appsettings is working, but im not the one initializing itForgotten
@Forgotten I think Generic Host will read from multiple config sources and it has priority you can find them here learn.microsoft.com/en-us/dotnet/core/extensions/… . In general environment variables will override the appsettings.json valuesCaitlin
See an example in #39574071Drape
A
15

The documentation is probably the best place to read up on all configuration methods that are available. To answer your specific question, the extension method you're after is defined on IConfigurationBuilder, therefore it has to be invoked before you build your host like so:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
       .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
       .ConfigureAppConfiguration(configurationBuilder =>
           { configurationBuilder.AddEnvironmentVariables(); }) 
       // here's where you add another `EnvironmentVariablesConfigurationSource`
                ;

However

a default EnvironmentVariablesConfigurationSource is already being injected for you by ConfigureWebHostDefaults, so you will end up having two providers.

I am assuming you want to inject a customised configuration instead, so you might need to remove the default one from the list first:

.ConfigureAppConfiguration(configurationBuilder =>
{
    configurationBuilder.Sources.Remove(
    configurationBuilder.Sources.First(source =>
        source.GetType() == typeof(EnvironmentVariablesConfigurationSource))); //remove the default one first
    configurationBuilder.AddEnvironmentVariables(); 
})

hopefully this gives you a starting point to explore further

Acetyl answered 11/2, 2020 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.