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