I’ve setup my active solution configuration to determine the appsettings file to use for the given build. I’m wondering if this is a recommended way the two in combination with each other, or if there an alternative best practice for managing configurations?
I deploy via publishing profiles within Visual Studio as I do not have a CICD pipeline.
There is an underlying assumption that Local
is Debug
and Staging
and Prod
are Release
builds.
The AppSettings are registered as follows:
<code> var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", true)
.Build();
</code>
<code> var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", true)
.Build();
</code>
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", true)
.Build();
Launch Settings:
<code> "https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "$(ConfigurationName)"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7184;http://localhost:5081"
},
</code>
<code> "https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "$(ConfigurationName)"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7184;http://localhost:5081"
},
</code>
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "$(ConfigurationName)"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7184;http://localhost:5081"
},
Project file to prevent configs from being published to unnecessary environments:
<code> <ItemGroup>
<Content Update="appsettings.Local.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Staging.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Prod.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.$(ConfigurationName).json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</code>
<code> <ItemGroup>
<Content Update="appsettings.Local.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Staging.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Prod.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.$(ConfigurationName).json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</code>
<ItemGroup>
<Content Update="appsettings.Local.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Staging.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.Prod.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.$(ConfigurationName).json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
My configuration files are:
And finally my configuration manager profiles are: