In ASP.NET Core 1.x the Configuration was initialized in the Startup class:
public class Startup | |
{ | |
public Startup(IHostingEnvironment env) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) | |
.AddEnvironmentVariables(); | |
Configuration = builder.Build(); | |
} | |
public IConfigurationRoot Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Add framework services. | |
services.AddMvc(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
loggerFactory.AddDebug(); | |
app.UseMvc(); | |
} | |
} |
If you create a new project in ASP.NET Core 2.0, your Startup class is almost empty and you cannot find any Configuration related code anywhere. Still if you run your application, the configuration is loaded and everything is working as expected.
So where is the magic?
public class Startup | |
{ | |
public Startup() | |
{ | |
} | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
app.UseMvc(); | |
} | |
} |
The Configuration is no longer found in the Startup class but is loaded and executed from the Program.cs file where it is part of the WebHost.CreateDefaultBuilder method.
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BuildWebHost(args).Run(); | |
} | |
public static IWebHost BuildWebHost(string[] args) => | |
WebHost.CreateDefaultBuilder(args) | |
.UseStartup<Startup>() | |
.Build(); | |
} |
This method does a lot of stuff out-of-the box including loading configuration data from the following sources:
- appsettings.json and appsettings.{environment}.json (e.g. appsettings.Development.json)
- User secrets
- Environment variables
- Command-line arguments
This explains the magic. But what if you want to change this? No worries, you can use the ConfigureAppConfiguration method to change the configuration.
WebHost.CreateDefaultBuilder(args) | |
.UseStartup<Startup>() | |
.ConfigureAppConfiguration((builderContext, config)=> { | |
IHostingEnvironment env = builderContext.HostingEnvironment; | |
config | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile("appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); | |
}) | |
.Build(); |
If you need the Configuration data in your Startup class, you can simply inject it through the constructor:
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
} |