Nuget 包内容文件未复制到 .NET Core 项目

Jér*_*VEL 8 c# nuget nuget-package .net-core

我正在尝试将 Nuget 包中的内容文件提取到引用我的包的项目中。

基于Justin Emgarten 的评论

Packages.config 项目使用 content 文件夹

Project.json/PackageReference/NETCore SDK 项目使用 contentFiles 文件夹

太好了,我创建了一个 .NET Core 2.1 控制台应用程序项目,并遵循了NuGet ContentFiles Demystified博客文章,该文章于 2016 年撰写,project.json但现在仍然可以使用。

我创建了一个图像,c:\dev\ContentFilesExample\contentFiles\any\any\images\dnf.png然后创建了一个c:\dev\ContentFilesExample\ContentFilesExample.nuspec文件并复制粘贴了内容:

<?xml version="1.0"?>
<package>
    <metadata minClientVersion="3.3.0">
        <id>ContentFilesExample</id>
        <version>1.0.0</version>
        <authors>nuget</authors>    <!-- The NuGet team authored this package -->
        <owners>nuget</owners>      <!-- The NuGet team owns this package -->
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>A content v2 example package.</description>
        <tags>contentv2 contentFiles</tags>
        <!-- Build actions for items in the contentFiles folder -->
        <contentFiles>
            <!-- Include Assets as Content -->
            <files include="**/images/*.*" buildAction="EmbeddedResource" />
        </contentFiles>
    </metadata>
</package>
Run Code Online (Sandbox Code Playgroud)

然后我用命令生成了 Nuget 包nuget pack ContentFilesExample.nuspec并使用 Nuget 包资源管理器打开它

在此处输入图片说明

太好了,我的照片按预期在那里。

现在是最后的非工作步骤。我在我的 .NET Core 2.1 项目中安装了这个 Nuget 包,但缺少图像。我的项目根目录下没有图像,obj文件夹和文件夹中都没有bin

我试图关闭并重新打开视觉工作室,如某些评论中所述,但这并没有解决问题。

我也尝试将我的 .NET Core 项目样式更改为PackageReference,但这并没有解决问题

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.1</TargetFramework>
        <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    </PropertyGroup>

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

    <ItemGroup>
        <None Update="appsettings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

那么我做错了什么?.NET Core 真的支持 Nuget 包中的内容文件吗?

谢谢

Jak*_*ake 5

nupkg 应该有contentFiles/目录:
努普克

安装到netcore项目:
在此输入图像描述

它不会是使用项目中的松散文件,而是EmbeddedResource构建项目时的松散文件:
在此输入图像描述


Ale*_*lex 5

经过数小时的谷歌搜索以寻求解决方案后,我最终解决了这个问题,所以我决定写另一个答案来澄清事情,因为 MS 文档很烂

  1. <files>.nuspec文件中的元素用于打包程序。它告诉 nuget 要打包哪些文件(如果<files>您的 nuspec中没有元素 - nuget 将使用目录命名约定)
  2. <contentFiles>element 是供消费者使用的- 它告诉如何以及何时提取文件。
  3. 如果你想有一个文件,安装后出现在你的项目-你要收拾了它target,说contentFiles/any/any这里的“任何/所有”部分讲述的NuGet它是任何语言和任何框架。
  4. (可选)如果您希望此文件与旧的 nuget 模式一起使用,则使用 packages.config - 您必须再次打包,这一次,第二次 - 到“内容”文件夹,以便年长的消费者仍然可以使用它。

努斯派克

  <metadata>
   ...
    <contentFiles>
      <files include="**/myfile.cs" />
    </contentFiles>
  </metadata>
  <files>
      <file src="myfile.cs" target="contentFiles\any\any" /> <!-- this is for new format -->
      <file src="myfile.cs" target="content" /> <!-- this is for the old format -->
  </files>
Run Code Online (Sandbox Code Playgroud)

附注。在我的博客文章更多的细节在这里