首页 > 解决方案 > NuGet - 如何在消费应用程序构建输出中包含应用程序 app.config 文件?

问题描述

如何让我的 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\24.0.0\contentFiles\any\any\assets\NuGetConfigInclude。 exe.config 1 活动

标签: .netnugetpackagereference

解决方案


该解决方案有效:

<?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 的文件夹中。


推荐阅读