When you add an appsettings.json file to a project, the following is inserted in its .csproj file:
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
with CopyToOutputDirectory being set to either PreserveNewest or Always to ensure the file is copied to the bin\ directory on build. I desire this behaviour for all projects in my repository, so I moved the above lines from the .csproj to a Directory.Build.props file in the repo root. Unfortunately this doesn't work - the directive appears to be ignored by MSBuild and Visual Studio, and the file is indeed not present in the output dir:
I then added a diagnostic message in Directory.Build.props to ensure that appsettings.json exists, and verified that this message appears in VS's Build output for the relevant project:
<Target Name="TestMessage" AfterTargets="Build">
<Message Condition=Exists('appsettings.json')"
Text="appsettings.json exists for $(MSBuildProjectFullPath)"
Importance="high"/>
</Target>
My next thought was that it was something to do with relative versus absolute file paths, so I replaced the relative 'appsettings.json' with its fully-qualified path of $([System.IO.Path]::Combine($(MSBuildProjectDirectory), 'appsettings.json')) - but the result is the same.
Running dotnet msbuild -v:diag seemingly confirms that MSBuild is indeed ignoring the directive from Directory.build.props:
None
.editorconfig
appsettings.json
whereas with that directive in the .csproj the output is:
None
.editorconfig
appsettings.json
CopyToOutputDirectory = PreserveNewest
What am I missing, or doing wrong?
