NuGet - 如何在消费应用程序构建输出中包含应用程序 app.config 文件?

NuGet - How to include the application app.config file in the consuming applications build output?

如何让我的 NuGet 包将其 app.config 包含在使用 NuGet 包的项目的构建输出目录中?目前它只复制 NuGetConfigInclude.exe 但我需要它也有 NuGetConfigInclude.exe.config 文件。请注意,消费应用程序在 .csproj 文件中使用 PackageReference。它不使用 packages.config.

NuGetConfigInclude.nuspec:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>NuGetConfigInclude</id>
    <version>1.0.0.0</version>
    <title>NuGetConfigInclude</title>
    <authors>NuGetConfigInclude</authors>
    <owners>NuGetConfigInclude</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>NuGetConfigInclude</description>
    <releaseNotes>Test Summary</releaseNotes>
    <copyright>Copyright 2020</copyright>
    <tags>TEST</tags>
  </metadata>
</package>

正在使用应用程序项目文件:

  <ItemGroup>
    <PackageReference Include="NuGetConfigInclude">
      <Version>1.0.0</Version>
    </PackageReference>
  </ItemGroup>

更新:

我已经设法让 nuspec 将文件包含在包中,但在正在尝试编译配置文件的消费项目中:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>NuGetConfigInclude</id>
    <version>24.0.0.0</version>
    <title>NuGetConfigInclude</title>
    <authors>NuGetConfigInclude</authors>
    <owners>NuGetConfigInclude</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>NuGetConfigInclude</description>
    <releaseNotes>Test Summary</releaseNotes>
    <copyright>Copyright 2020</copyright>
    <contentFiles>
      <files include="contentFiles\any\any\assets\**" buildAction="None" copyToOutput="true" flatten="false" />
    </contentFiles>
    <tags>TEST</tags>
  </metadata>
  <files>
    <file src="bin\Debug\NuGetConfigInclude.exe.config" target="contentFiles\any\any\assets"/>
  </files>
</package>

构建消费项目时的示例错误:

严重性代码说明项目文件行抑制状态 错误 CS1022 类型或命名空间定义,或预期文件结束 NuGetConsumer C:\Users\GuestAcct.nuget\packages\nugetconfiginclude.0.0\contentFiles\any\any\assets\NuGetConfigInclude.exe.config 1 Active

此解决方案有效:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>NuGetConfigInclude</id>
    <version>29.0.0.0</version>
    <title>NuGetConfigInclude</title>
    <authors>NuGetConfigInclude</authors>
    <owners>NuGetConfigInclude</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>NuGetConfigInclude</description>
    <releaseNotes>Test Summary</releaseNotes>
    <copyright>Copyright 2020</copyright>
    <contentFiles>
      <files include="**/*.*" buildAction="None" copyToOutput="true" flatten="true" />
    </contentFiles>
    <tags>TEST</tags>
  </metadata>
  <files>
    <file src="bin\Debug\NuGetConfigInclude.exe.config" target="contentFiles\any\any\"/>
  </files>
</package>

请务必注意,如果您的消费项目正在使用 PackageReference,则需要将文件复制到名为 contentFiles 的文件夹中。