Here's what I tried:
hostsettings.json to the root of the executing project:{
"Environment": "Production"
}
Nada.
How in the heck do you specify a production machine in order to use the appsettings.Production.json file?
Nevermind, I figured it out. I'm dumb.
First, check your Properties/launchSettings.json file for the value of "DOTNET_ENVIRONMENT". That's probably enough for most, including me.
It will, in fact, also look for a hostsettings.json file in the project root by default. It was just being overridden in my case by the launchSettings environment variable declaration.
However, if you want to use the JSON approach I mentioned in the question, but with something other than the default file name or location, you gotta call ConfigureHostConfiguration in your IHostBuilder chain and add the file explicitly.
Like so:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(
builder =>
{
builder.AddJsonFile("arbitraryFileName.json");
})
.ConfigureServices(
(hostContext, services) =>
{
services.AddHostedService<Worker>();
services.Configure<Settings>(
hostContext.Configuration.GetSection("Settings"));
});
It does indeed default to "DOTNET_" as the prefix instead of "ASPNETCORE_", so make sure both are set on your production machine/VM/service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With