Nuget package contentFiles not copied to .NET Core project

亡梦爱人 提交于 2019-12-19 03:13:18

问题


I'm trying to extract content files from a Nuget package to a project referencing my package.

Based on Justin Emgarten's comment

Packages.config projects use the content folder

Project.json/PackageReference/NETCore SDK projects use the contentFiles folder

So ok great, I created a .NET Core 2.1 Console Application project and followed the NuGet ContentFiles Demystified blog post which was written in 2016 at the time of project.json but should still work nowadays.

I created an image at c:\dev\ContentFilesExample\contentFiles\any\any\images\dnf.png then created a c:\dev\ContentFilesExample\ContentFilesExample.nuspec file and copy pasted the content:

<?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>

Then I generated the Nuget package with the command nuget pack ContentFilesExample.nuspec and opened it using Nuget Package Explorer

Great my picture is there as expected.

And now the final non-working step. I install this Nuget package in my .NET Core 2.1 project but the image is missing. No trace of the image in the root directory of my project, neither in the obj folder nor in the bin folder.

I tried to close and re-open visual studio as stated in some comments somewhere but that didn't solve the issue.

I also tried to change my .NET Core project style to PackageReference but again, this didn't solve the issue

<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>

So what am I doing wrong? Are content files in Nuget packages really supported by .NET Core?

Thank you


回答1:


nupkg should have contentFiles/ directory:

Install to netcore project:

It won't be a loose file in the consuming project, it will be an EmbeddedResource when you build the project:



来源:https://stackoverflow.com/questions/51959638/nuget-package-contentfiles-not-copied-to-net-core-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!