aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
552 stars 312 forks source link

reading from appsettings in Program.cs using getsetting #1556

Closed imxavi closed 5 years ago

imxavi commented 5 years ago

Hi, I have this:

        public static void Main(string[] args)
        {
            _config = LoadConfiguration(args);
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var hostBuilder = WebHost.CreateDefaultBuilder(args).UseConfiguration(_config);

            var settingName = "SHUTDOWNTIMEOUTSECONDS";
            var settingStrShutdownTimeout = hostBuilder.GetSetting(settingName);

            hostBuilder
            //....
                .UseStartup<StartUp>();

            return hostBuilder;
        }

         static IConfiguration LoadConfiguration(string[] args)
        {
            var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            .AddEnvironmentVariables();
            if (args != null)
            {
                builder.AddCommandLine(args);
            }
            return builder.Build();
        }

I was expecting 2 things: a) get SHUTDOWNTIMEOUTSECONDS from appsettings, but it gets null b) if SHUTDOWNTIMEOUTSECONDS is in appsettings.json with 30 and launchSettings.json (env var) with 50, I was expecting 50 due to Loadconfiguration order. i.e, env var overrides previous value set. but I am getting always what is in appsettings. Is I use new WebHostBuilder()instead of WebHost.CreateDefaultBuilder is the same behavior. Some light on what I am doing wrong please. Thanks

Tratcher commented 5 years ago

Can you show appsettings.json and the related environment variables?

Tratcher commented 5 years ago

Also, try making appsettings.json optional: false to make sure the file is being found correctly.

imxavi commented 5 years ago

Hi, sorry let me explain better. I did 2 scenarios: A) using just CreateDefaultBuilder without UseConfiguration var hostBuilder = WebHost.CreateDefaultBuilder(args);

var settingName = "SHUTDOWNTIMEOUTSECONDS"; var settingStrShutdownTimeout = hostBuilder.GetSetting(settingName);

B) add UseConfiguration with the code above in the initial question. files are with:

- launchSettings.json:
      "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS" : "50"
  }
- appsettings.json:
"SHUTDOWNTIMEOUTSECONDS": 30

I was expecting to get 50 because in LoadConfiguration we do AddEnvironmentVariables() after AddJsonFile("appsettings.json"...)

Thanks in advance.

Tratcher commented 5 years ago

AddEnvironmentVariables isn't working because you need to specify the prefix in order for the matching to work correctly: AddEnvironmentVariables("ASPNETCORE_").

hostBuilder.GetSettings reads only from the host config, where appsettings isn't loaded later until the app's config is build. That's why UseConfiguration works, it loads the config immediately into the host config.

imxavi commented 5 years ago

Hi, understood. Thank You.