Starting with ASP.NET Core 2.0, we perform configuration in the Program
class when building the WebHost
instance. An example of such a setting:
return new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((builderContext, config) => { IHostingEnvironment env = builderContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); })
Among other things, this allows you to use the configuration directly in the Startup
class, getting an instance of IConfiguration
using the constructor injector (thanks, built-in DI container):
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } ... }
source share