Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotnet publish doesn´t publish correct appsettings.{env.EnvironmentName}.json

When I issue the following command in the command line:

dotnet publish -o "./../output" -c Release

The dotnetcli publishes the project correctly. However, it does not copy the appsettings.Production.json file, only the appsettings.json.

Why is this? I have googled around and read the official core docs, but haven't found how the correct environment appsettings.json is supposed to end up in the publish output.

Should I copy appsettings.Production.json manually to the published folder?

like image 670
peco Avatar asked Jun 16 '16 11:06

peco


People also ask

Where is launchSettings json after publish?

In the Properties folder in an ASP.NET Core project, you can find the launchSettings. json file, which contains settings that control how your web app is started on your development machine.

What is the difference between Appsettings json and Appsettings development json?

NET Core and as far as I see from my search on the web, appsettings.Development. json is used for development config while developing the app and appsettings. Production. json is used on the published app in production server.

How do I add Appsettings to json?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.


1 Answers

Update: For current (new) .csproj format the CopyToPublishDirectory attribute should be used. It determines whether to copy the file to the publish directory and can have one of the following value:

  • Always,
  • PreserveNewest
  • Never

So add next section into your .csproj:

<ItemGroup>    <None Include="appsettings.Production.json" CopyToPublishDirectory="Always" /> </ItemGroup> 

Look into @nover answer and SO Exclude or include files on publish for more information about file's control during publishing.


"In your project.json file you have the section publishOptions with subsection include, where you already have some files like "appsettings.json":

"publishOptions": {   "include": [     "appsettings.json",     "hosting.json",     "project.json",     "web.config"   ] }, 

You should add "appsettings.Production.json" into this array.

Updates based on comments:

  • Keep in mind, that all the appsettings.*.json files like appsettings.development.json, appsettings.staging.json and appsettings.production.json will always end up in all environments. You cannot simply handle this using project.json, as it does not support any condition rules. This will be changed in future, when project.json will be replaced back to msbuild and .csproj. If this is critical for your app, consider to use another configuration store, like Environment Variable, database, etc.

  • Note, that order is important, as determine which settings will be applied if they exist in multiple locations. From documentation:

    The order in which configuration sources are specified is important, as this establishes the precedence with which settings will be applied if they exist in multiple locations. In the example below, if the same setting exists in both appsettings.json and in an environment variable, the setting from the environment variable will be the one that is used. The last configuration source specified “wins” if a setting exists in more than one location. The ASP.NET team recommends specifying environment variables last, so that the local environment can override anything set in deployed configuration files.

like image 74
Set Avatar answered Oct 21 '22 21:10

Set