Accessing the IHostingEnvironment in static main of ASP.NET Core
After some discussion on aspnetcore.slack.com in the #general channel (May 26,2016 12:25pm), David Fowler said "you can new up the webhostbuilder and call getsetting(“ environment”)" and "hosting config != app config".
var h = new WebHostBuilder();
var environment = h.GetSetting("environment");
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddEnvironmentVariables();
var configuration = builder.Build();
If you want to store the password for the Https certificate finally in the User Secrets add the following lines in the appropriate sections in Main of Program.cs:
var config = new ConfigurationBuilder()
.AddUserSecrets("your-user-secrets-id") //usually in project.json
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel(options=> {
options.UseHttps("certificate.pfx", config["your-user-secrets-id"]);
})
The user secrets have to be passed in directly, because the configuration of project.json for "userSecretsId" is not yet accessible at this stage.