Dotnet 发布不能发布正确的应用程序设置

当我在命令行中发出以下命令时:

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

dotnetcli正确地发布项目。但是,它不复制 appsettings.Production.json文件,只复制 appsettings.json文件。

为什么会这样?我四处搜索并阅读了官方的核心文档,但是没有找到正确的环境 appsettings.json应该如何在发布输出中结束。

我是否应该手动将 appsettings.Production.json复制到已发布的文件夹?

71955 次浏览

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.

In your project.json there is a section publishOptions. This lists all the files and folders that will be included when you publish. You will need to update yours to look something like this

{
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"appsettings.Production.json",
"web.config"
]
},
}

You can also use globbing patterns, so you should find this works too (I haven't tested this one)

{
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings*.json",
"web.config"
]
},
}

For the new csproj project format you have to add a new ItemGroup with the content

<ItemGroup>
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="appsettings.Production.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

In case you have multiple appsettings.{env}.json files simply repeat the Content tag inside the same ItemGroup and all your settings files will end up in the publish folder.

As mentioned in the comments an even cleaner solution is to use a wildcard include:

<ItemGroup>
<Content Include="appsettings*json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

And all your appsettings files will be published!

After Visual Studio 2017 15.3

Edit the .csproj file to manually exclude files/folder from being published

<ItemGroup>
<Content Remove="appsettings.Development.json" />
</ItemGroup>

ref: https://www.danielcrabtree.com/blog/273/fixing-the-duplicate-content-error-after-upgrading-visual-studio-2017

original source

Discovered a three-step build in Visual Studio approach for publishing environment-specific appsetting files (Windows, PowerShell).

  • appsettings.json
  • appsettings.Development.json
  • appsettings.Staging.json
  • appsettings.Production.json

This approach will publish

  • appsettings.json and
  • appsettings.$(ASPNETCORE_ENVIRONMENT).json.

Step 1. Update csproj:

  <!-- App Settings -->
<ItemGroup>
<Content Remove="appsettings.json" />
<Content Remove="appsettings.*.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json" CopyToOutputDirectory="Always" />
<Content Include="appsettings.$(ASPNETCORE_ENVIRONMENT).json" DependentUpon="appsettings.json" CopyToOutputDirectory="Always" />
</ItemGroup>

Step 2. Set an environment variable in PowerShell:

  # Read
[Environment]::GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "User")
# Output: empty string if not set or 'Staging' in my case
# Set environment variable "User" or "Machine" level
[Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Staging", "User")

Step 3. Then close and reopen Visual Studio solution to enable Visual Studio to see the environment variable and reload project structure accordingly.

  • Now appsettings.json is a parent and appsettings.Staging.json is a nested file.
  • If you set another environment (for example "Production") and then close and Visual Studio and reopen your solution, then you will appsettings.json as a parent and appsettings.Production.json as a nested file.

enter image description here

Final step. Run publishing.

enter image description here

Note: publishing profile environment variables do not affect publishing configuration. This approach uses PowerShell to set an environment variable and enables environment-specific publishing. Please see link for more details on environment variables.

enter image description here

VS 2019 (Version 16.4.1) has option to publish only the appsettings.json on right click on the file:

publish-json

It will take your connection from the publish profile.

I found that you don't need to manually modify the project file to add in the items. Just set the "Build Action" to "Content" for these items in file properties. They get copied to the build output AND the publish folder. They're also included in the Application Files folder with the .deploy extension if you're publishing for ClickOnce.

enter image description here