I have three configuration files, one for each environment:
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.
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 solution explorer right click on your file that want to be copied to output (in your case appsettings.dev.json
) and hit Properties, In Properties window pane set Copy To Output Directory option to Copy Always. By doing so, every time you build your project, your file will be copied into output directory (your project bin or release directory depending on your build profile)
If you want to copy a file to your output folder you can add this to your csproj file:
<ItemGroup>
<None Update="appsettings.IntegrationTests.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
This works with dotnet build/test/run vscode & visual studio
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.