18

I have three configuration files, one for each environment:

  1. appsettings.json -> production
  2. appsettings.dev.json -> development
  3. 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?

J. Lennon
  • 3,121
  • 4
  • 31
  • 63
Alex G.
  • 1,773
  • 3
  • 21
  • 31
  • You've got an answer from Ivan. Maybe it's a good idea to indicate if that works for you? – Maarten Bodewes Jul 28 '16 at 17:30
  • 1
    I couldn't find a solution to deploy a specific file per environment. You could configure to deploy all the project*.json files -http://stackoverflow.com/questions/37858312/dotnet-publish-doesn%C2%B4t-publish-correct-appsettings-env-environmentname-json But that's not good option, the perfect scenario is the 1 to 1 file per environment (configuration name) - the bounty is open :) – J. Lennon Sep 12 '16 at 21:13
  • See [this answer](https://stackoverflow.com/a/54216774/10685590) for a possible solution of environment specific appsettings. – Ben Jan 16 '19 at 12:19

6 Answers6

13

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.

enter image description here

Jess
  • 20,424
  • 18
  • 108
  • 130
8

Including this in "project.json" works for me:

...
"publishOptions": {
  "include": [
    "wwwroot",
     ...
    "appsettings.json",
    "appsettings.Development.json",
    ...
  ]
},
...
Fabricio Koch
  • 1,231
  • 8
  • 19
  • 1
    Didn't work for me. It worked for one time cause project.json was changed and it caused project to rebuild. Only solution working — call rebuild instead of build. It works with both compile options and publishOptions. – VorobeY1326 Nov 04 '16 at 08:31
7

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);
Ivan Zaruba
  • 3,495
  • 4
  • 16
  • 27
  • 2
    This will prevent the exception from being thrown, but doesn't answer the question of how to make sure the `.dev.json` file is copied. – Philip Pittle Sep 12 '16 at 21:54
  • .. And check your .vscode/launch.json -> "cwd" path. Setting this from "${workspaceRoot}" to "${workspaceRoot}\src\myproject" worked for me. – Vladislav Feb 16 '17 at 10:19
6

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

Marius
  • 8,221
  • 7
  • 43
  • 68
  • +1 - Originally had the same as this except the element name was . Everything else was the same - but it didn't work. Changing it to None got it working – Jacques Jan 28 '21 at 08:22
5

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)

Code_Worm
  • 2,669
  • 2
  • 25
  • 29
2

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.

djangojazz
  • 12,570
  • 9
  • 48
  • 82