Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

asp.net core - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...