0
votes

I'm deploying a apt.net core 2.0 web api as a windows service. I have a appsettings file for each of my environment (ex: appsettings.Development.json).

However, my environment specific appsetting file is not loaded into my configurations. here is the code :

        var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
        var pathToContentRoot = Path.GetDirectoryName(pathToExe);       

        return WebHost.CreateDefaultBuilder(args)
                .UseContentRoot(pathToContentRoot)
                .ConfigureAppConfiguration((builderContext, config) =>
                {
                    IHostingEnvironment env = builderContext.HostingEnvironment;

                    config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddJsonFile("appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
                        //.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)

                })
                .UseStartup<Startup>()
               .UseUrls(myUrl)              
               .Build();

I tried to load a environment specific configuration file by hard coding (code in comment) the value, WHICH IS WORKING. I can also guarantee that there's no typo between my environment name and the file name.

My environment name is set via environment variable on the dployment machine.

1

1 Answers

0
votes

second .addJson should be

.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

You were missing the $ before the string