Automatically set appsettings.json for dev and release environments in asp.net core?
You may use conditional compilation:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
#if SOME_BUILD_FLAG_A
.AddJsonFile($"appsettings.flag_a.json", optional: true)
#else
.AddJsonFile($"appsettings.no_flag_a.json", optional: true)
#endif
.AddEnvironmentVariables();
this.configuration = builder.Build();
}
How to set appsettings.json automatically for deployment in ASP.NET Core?
First of all as docs state: launchSettings.json
is only used on the local development machine.
As for using configuration file - by default next files are used:
appsettings.json
using the JSON configuration provider.appsettings.Environment.json
using the JSON configuration provider. For example,appsettings.Production.json
andappsettings.Development.json
.
The environment for ASP.NET Core app determined next way:
To determine the runtime environment, ASP.NET Core reads from the following environment variables:
DOTNET_ENVIRONMENT
ASPNETCORE_ENVIRONMENT
whenConfigureWebHostDefaults
is called. The default ASP.NET Core web app templates callConfigureWebHostDefaults
. TheASPNETCORE_ENVIRONMENT
value overridesDOTNET_ENVIRONMENT
.
So the usual approach is to set ASPNETCORE_ENVIRONMENT
environment variable to needed value on target host (machine, docker container, etc...)
Automatically for dev and release in .net core
Based on the guide you link to, you shouldn't need any explicit loading of the app settings files. It will, by default, load the appsettings.json
and appsettings.<Environment>.json
automatically. It will also automatically load any environment variables with an ASPNETCORE_
prefix it finds. (or DOTNET_
for generic hosts)
I would remove the redundant code. let the builder do its job, and ensure the environment variables are set and available when your app starts. If you can't find or get any value or the value you expect with Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
then something's wrong with how they're being set.
Also, appsettings.json
will load regardless of any issues with missing vars if it's present so I wouldn't put the production connection string there. Rather, put your local or dev connection string there and let the production value overwrite the dev/default value so you don't accidentally default to production.
ASP.NET Core app does not use setting from the appsettings.json, after trying to publish to Azure
By running locally I am taking it that you mean in debug mode from Visual Studio. Doing the following two things should fix the issue:
Search the solution for "aspnet-53bc9b9d-9d6a-45d4-1122-2a2761773502" and correct anywhere that is set (settings files or otherwise).
Delete the debug and obj folders in the project directory. Clean and rebuild the solution.
The other possibility is that there is an environment variable set on your machine for the connection string, but it seems unlikely that this would be there without you already being aware.
.Net Core automatic change between appsettings by selecting configuration
You can have different appsettings for different environments like:
appsettings.Development.json
appsettings.Staging.json
appsettings.Production.json
- etc. ...
and then add different profiles with different environment to launch your application in launchSettings.json
launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49364",
"sslPort": 0
}
},
"profiles": {
"IIS Express Development": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express Staging": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
},
"IIS Express Production": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
Related Topics
Can Itextsharp.Xmlworker Render Embedded Images
Cancellation Token in Task Constructor: Why
Why There Are 5 Versions of Timer Classes in .Net
System.Lazy<T> with Different Thread-Safety Mode
Xml Serialization and Inherited Types
Which Version of C# am I Using
Invalid Length for a Base-64 Char Array
Exclude Property from Serialization via Custom Attribute (JSON.Net)
Is There a Jquery-Like CSS/HTML Selector That Can Be Used in C#
How to Directly Execute SQL Query in C#
How to Display a Popup from a Webbrowser in Another Window I Created
How to Run a C# Application at Windows Startup
When Should I Use "Using" Blocks in C#
Why Are Extension Methods Only Allowed in Non-Nested, Non-Generic Static Class
What Is the Minimum Client Footprint Required to Connect C# to an Oracle Database