Reputation: 3204
I am not able to get my published ASP.net Core application to switch to using the connection string in my appsettings.production.json file.
I have setup the launchSettings to use the ASPNETCORE_ENVIRONMENT environment variable for my Development and Production profiles. When I switch the profiles and run them in visual studio, my connection string changes properly.
When I run the published app on my Ubuntu server, it does not switch. I have set the ASPNETCORE_ENVIRONMENT variable to "Production" on my server. I have also verified that both the appSettings.json and appSettings.production.json exist in the root directory for the application.
My appsettings.json file:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=dev;database=testdb;sslmode=none",
}
}
my appsettings.production.json file:
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=prod;database=testdb;sslmode=none"
}
}
My launchSettings.json file:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50824/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express (Production)": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
My Startup.cs:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
Console.WriteLine("Root Path: " + env.ContentRootPath);
Console.WriteLine("Connection String: " + Configuration.GetConnectionString("DefaultConnection"));
}
I have referenced these questions already but no luck:
asp.net core 1 appsettings.production.json not updating connection strings
dotnet publish doesn´t publish correct appsettings.{env.EnvironmentName}.json
Upvotes: 11
Views: 7910
Reputation: 3204
As it turns out this "Note" in the official documentation is VERY important:
On Windows and macOS, the specified environment name is case insensitive. Whether you set the variable to Development or development or DEVELOPMENT the results will be the same. However, Linux is a case sensitive OS by default. Environment variables, file names and settings should assume case sensitivity for best practice.
Mainly the line "Linux is a case sensitive OS by default"!!!!! Whoops :)
Once I changed my environment variable to "production" instead of "Production", it worked.
Further Explanation:
The key is understanding this line of code in the Startup.cs Startup method:
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
It replaces the {env.EnvironmentName} with your environment variable, so if you are operating in linux, it needs to match your file name exactly. In my case "appSettings.production.json" so ASPNETCORE_ENVIRONMENT must be "production".
Upvotes: 6