So I'm trying to get a .NET Core API application running with different environment settings. I've been reading the documentation and as far as I can tell I've followed the instructions. Yet when I run the service outside of VS it blows up due to not being able to find a connection string. I'm clearly missing something basic.
I've setup two profiles in launchSettings.json "Development" and "Staging"
"profiles": {
"IIS Express": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Development": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Staging": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
}
I have the following configuration files.
appsettings.json
appsettings.Development.json
appsettings.Staging.json
I also have the following code in in my Startup.cs:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
When I debug this I can see that .AddJsonFile($"appsettings.{env.EnvironmentName}.json" is loading the expected file. However, when I publish this to a directory and attempt to run it the connectionString value us null.
appsettings.json doesn't contain a value for the connection string. It is in the two environment dependent files, defined like:
"DefaultConnection": {
"ConnectionString":
"Server=name;Port=3307;Database=name;User Id=name;Password=name;"
},
Since this all works inside VS I'm sure this is all correct. However when I try to run it, BOOM!
dotnet MyDll.dll --launch-profile Staging
I've just noticed that --launch-profile only works with dotnet run and that when I try to run the application it's looking for a Production.json file. How do I use the various profiles with just the dotnet command, not the dotnet run command?