I have three configuration files, one for each environment:
- appsettings.json -> production
- appsettings.dev.json -> development
- appsettings.stg.json -> staging
If I set ASPNETCORE_ENVIRONMENT to dev, I get a runtime exception complaining about not being able to find appsettings.dev.json. I tried adding
"copyToOutput": [
"appsettings.dev.json"
]
to the buildOptions
section in project.json but it doesn't seem to have any effect.
Is there another way I can force appsettings.dev.json to be copied to the output directory?
Including this in "project.json" works for me:
...
"publishOptions": {
"include": [
"wwwroot",
...
"appsettings.json",
"appsettings.Development.json",
...
]
},
...
Have you set the base path where appsettings.dev.json is placed?
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
In my case, appsettings.json is not being copied for unit tests.
If you right click the file and choose Properties, this dialog will come up. Change Build Action to Embedded resource and the file will be copied to the bin folder for the unit test to pick up.
Assumes you are using Visual Studio:
Typically for services in the past I have had other devs set up 'Build Events' that ensure that certain things in the project get built. Typically it would be in ASP.NET project properties for Visual Studio 2015 as an example: >'Build Events'(left pane)>Click 'Edit Post-build'. Write in command line instructions of how the project should build to output. EG:
mkdir "$(ProjectDir)bin\Setup"
create a directory under bin called 'Setup'
Del "$(ProjectDir)bin\Setup\*.* /Q"
Deletes any existing items in the newly created bin
copy "$(TargetDir)*.dll" "$(ProjectDir)bin\Setup"
Copies dll's from build location to the new setup location
Now if you are doing a build process where your 'Configuration Manager' is doing a different output for each environment and you just want each of them at all times you would probably need to traverse up a level, find those bins by their name of 'dev' or 'stg' and then copy them back. You may be able to create or list a variable that VS knows for the environment that matches to your JSON data as well.