6
votes

I'm building a DotNet Core console application. I have noticed that when I publish the application to the file system, the appsettings.json file is NOT included in the output files.

I've found that changing the copy option on the appsettings can make it appear, but it doesn't by default.

Shouldn't appsettings be included as a separate file in a console application so that you can configure the app on the fly?

1
appsettings.json is an json file and nothing more. So it's OK to setup Build action and Copy to output directory actions manually.Ilya Chumakov
Seems like default action for any JSON file added is "Do not copy" regardless the name ...Ignas
The .csproj should have the following: <ItemGroup><Content Include="appsettings.json;appsettings.development.json"><CopyToPublishDirectory Condition="Exists(%(Identity))">PreserveNewest</CopyToPublishDirectory></Content></ItemGroup>Boggin
I wonder how others deal with this? I mean don't others write console apps and don't they need settings? I always wonder about everybody else's life that develop everyday.Paul Duer
Well, some if not most settings are going to be environment-specific, and as such should not be deployed with the app. In many cases, the place where config is stored is completely different (it's common to have a json file in dev, and env variables in prod).Bertrand Le Roy

1 Answers

3
votes

I think the file is left behind by default for security reasons (password in a connectionString ?).
You can use the this answer to get your files copied.

By default the appsettings.json is part of the default <ItemGroup> of the project, so add the <None> tag for the copy inside the one listing the <PackageReference> nodes.

<None Include="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None>

You can use this link for more information about safe storage of app secrets in .Net Core.