Reputation: 3907
I'm quite new to .NET Core application, and I was wondering if there's a simple way to deploy the right application.json based on the profile. In .NET Standard application it was quite simple since we have web.config transformation/Slowcheeta but It doesn't seems to work with .NET Core Console app.
I've also read online that with ASP.NET Core application, we can use
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) <--this
.AddEnvironmentVariables();
Configuration = builder.Build();
But on .NET Console app I don't have an env.EnvironmentName
I've also read that someone uses a #IF-#ENDIF
statement to switch but this requires to put hands on code if a new publish profile is created
Any suggestion_
Upvotes: 3
Views: 5431
Reputation: 3539
To improve on the answer I have upvoted by @MarkusDresch, I wonder if OP says this did not work still because they do not have something setting up the correct environment. You need to have this being set correctly such as from an env variable of hostsetting.json:
var host = new HostBuilder()
.ConfigureHostConfiguration(builder =>
{
builder.AddJsonFile("hostsettings.json", optional: true);
})
.ConfigureAppConfiguration((hostContext, builder) =>
{
builder.AddJsonFile("appsettings.json");
builder.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
})
With this example the hostsettings.json would have something like:
{
"environment": "Development",
}
Upvotes: 5
Reputation: 1109
You do have access to Environment. That should be enough:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
Upvotes: 1
Reputation: 5574
You can use Microsoft.Extensions.Configuration.Json (https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/)
The equivalent code would look something like this:
var builder = new HostBuilder();
builder.ConfigureAppConfiguration((ctx, b) => b
.AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json"));
As far as i know this is only available for .NET Core 2.1. HostBuilder works pretty similar to ASP.NET WebHostBuilder and is available in NuGet package Microsoft.Extensions.Hosting.
Upvotes: 2
Reputation: 2756
You can try to do it like that:
public static void Main(string[] args)
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
Please remember about adding Microsoft.Extensions.Configuration.Json
nuget package.
There is also a way to get ASPNETCORE_ENVIRONMENT
like this:
new ConfigurationBuilder().AddEnvironmentVariables()
and finding that setting there.
And of course, you need to copy those configs to the output folder so that why will be accessible.
Upvotes: 1