Nuget包contentFiles未复制到.NET Core项目 [英] Nuget package contentFiles not copied to .NET Core project

查看:264
本文介绍了Nuget包contentFiles未复制到.NET Core项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



基于



太棒了,我的照片在那里。



现在是最后一个无效的步骤。我在.NET Core 2.1项目中安装了此Nuget程序包,但是缺少该映像。在项目的根目录中,无论是在 obj 文件夹中还是在 bin 文件夹中,都没有图像的痕迹。



我试图关闭并重新打开Visual Studio,如某处的一些评论中所述,但这并不能解决问题。



我还尝试将.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>

那么我在做什么错呢? NET Core是否真的支持Nuget包中的内容文件?



谢谢

解决方案

nupkg应该具有 contentFiles / 目录:



安装到netcore项目:



不会消费项目中的一个宽松文件,当您构建项目时,它将是 EmbeddedResource


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

解决方案

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:

这篇关于Nuget包contentFiles未复制到.NET Core项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆