I am having a console application, which is using appsettings.json for holding the configuration values.
In Visual Studio IDE , when I set AlwaysCopy for appsettings.json, it will be copied to the DEBUG folder, as part of BUILD.
But, in .net core, when I run dotnet build
, it is not copying appsettings.json to DEBUG folder.
I am already having below xml configuration added to .csproj
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
When I do dotnet publish
, it is getting copied to publish folder. But, it is not happening when I do dotnet build
. Can you please tell, why it is not happening & what should I do to copy the appsettings.json to DEBUG folder?
Found the answer in another thread is Stackoverflow(The difference between build and publish in VS?). In .NET Core, build & publish are different, as compared to .NET Framework.
Building .NET Framework applications will generate the same files as Publish. It will create all the dependencies as binaries including external dependencies(NuGets for instance). So the product of dotnet build is ready to be transferred to another machine to run
Building .NET Core applications, if the project has third-party dependencies, such as libraries from NuGet, they're resolved from the NuGet cache and aren't available with the project's built output. Therefore the product of dotnet build isn't ready to be transferred to another machine to run. You need to run Publish to get all 3rd party dependencies as binaries in output folder.
If we want a file to be part of publish, we need to add below attribute. In build, this file is not copied to the DEBUG/RELEASE folder.
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>