.Net Core 3.1 adding additional config.json file to configuration argument in Startup
You can do it in Program.cs
i.e. earlier in the pipeline rather than in Startup.cs
.
Example:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, config) =>
{
var env = hostContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Your approach should work, but needs a little bit of tweaking.
The configuration that you create needs to be added to the DI Container. The documentation for this is here.
I've achieved it as well in Azure Functions and other projects through the following:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(provider => new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddJsonFile("accountconstants.json", optional: true, reloadOnChange: true)
.Build());
}