为什么将NuGet包中的内容文件添加为.NET Core项目中的链接,如何防止这种情况发生? [英] Why are content files from NuGet packages added as links in .NET Core projects and how can you prevent that?

查看:29
本文介绍了为什么将NuGet包中的内容文件添加为.NET Core项目中的链接,如何防止这种情况发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发.NET标准库,并计划在.NET Core和.NET Framework项目中使用它.我的库依赖于第三个库,该库本身依赖于几个C二进制文件.C二进制文件必须复制到将引用我的库的项目的输出目录中.

我将二进制文件作为内容添加到我的库项目中,并且当NuGet包与 dotnet打包在一起时,它们被复制到 content contentFiles 目录中打包命令.这些文件也在 nuspec 文件中列出.


但是,当我安装NuGet软件包时,有两种不同的行为需要观察,具体取决于我是否将其安装到.NET Framework或.NET Core项目中:

.NET Framework

我添加到库项目中的二进制文件也添加到引用项目中,我可以将它们复制到输出目录中.这是我期望,想要和起作用的行为.

.NET Core

我添加到库项目中的二进制文件也被添加到引用项目为链接,该链接也可以运行,但在我的计算机上仅 >当然,因为默认情况下,NuGet软件包存储在Windows用户文件夹中.这会给其他将在不同计算机上的同一项目上工作的其他人带来问题,因为在这种情况下,文件的链接会断开.


我已经尝试过弄乱 nuspec 文件本身,但这并没有真正的帮助,因为在打包之后,我只得到了 nuspec 文件,这是由CI管道完成的.

在这种情况下,为什么.NET Framework和.NET Core项目的行为不同,并且可以在.NET Core项目中获得.NET Framework项目的行为?

解决方案

实际上,这是正常行为.

您看到的内容和contentFiles行为是正常的,它们的规则是由Microsoft设计的.

请参阅

注意:如果您的lib nuget名为 lib.1.0.0.nupkg ,则props文件应命名为 lib.props 文件,以便它将正常工作.它应该与 nuget_id 相同.

2)将它们添加到 lib.props 文件中:

 < Project><目标名称="CopyFiles";BeforeTargets ="Build"< ItemGroup><文件包含=" $(MSBuildThisFileDirectory).. \ Binary \ *.*></File></ItemGroup>< Copy SourceFiles ="@(文件)";DestinationFolder ="$(ProjectDir)Binary"</Copy></Target></Project> 

3)将它们添加到lib项目的 xxx.csproj 文件中.

 < ItemGroup><无包含="xxx \ **.**"(所有二进制文件)Pack ="true".PackagePath ="Binary"</None><无包含="build \ lib.props";Pack ="true"PackagePath ="build"</None></ItemGroup> 

4):在安装新版本的nuget软件包时,它将二进制文件复制到名为 Binary 的文件夹中,并将其复制到您的主项目中.

另外,当您安装此新版本的nuget软件包时,请有关这些步骤的类似问题.

I am developing a .NET Standard library and plan to use it in both .NET Core and .NET Framework projects. My library is dependant on a third library which itself is dependant on a few C binary files. The C binary files have to be copied to the output directory of the project which will be referencing my library.

I added the binary files to my library project as content and they are copied to the content and contentFiles directories when the NuGet package is packed with the dotnet pack command. The files are also listed in the nuspec file.


However when I install the NuGet package there are two different behaviours to observe depending if I install it to a .NET Framework or .NET Core project:

.NET Framework

The binary files I added to my library project are also added to the referencing project and I can copy them to the output directory. This is the behaviour that I expect, want and works.

.NET Core

The binary files I added to my library project are also added to the referencing project but as links which also works but only on my machine of course since the NuGet packages are stored in the Windows user folder by default. This create problems for anyone else that will work on the same project on a different machine as the links to the files are broken in that case.


I already tried messing around with the nuspec file itself but this doesn't really help since I only get the nuspec file after packing which is done by CI pipeline.

Why is the behaviour for .NET Framework and .NET Core project different in that case and is it possible to get the behaviour of a .NET Framework project in a .NET Core project?

解决方案

Actually, this is the normal behavior.

And what you saw of content and contentFiles's behavior is normal and the rule of them is designed by Microsoft.

Please refer to this similar issue and this one.

Content node will pack its content as the real files into the main project. This is for Net Framework projects with packages.config nuget management format.

While, ContentFiles node will pack its content as link format into the main project(Net Core and Net Standard projects). This is for PackageReference nuget management format.

So if you want this feature is the same as the Net Framework project on Net Core, you should use a <package_id>.props on net standard projects to get what you want.

Steps

1) create a file called <packages_id>.props file under the build folder on your net standard lib project.

Note: if your lib nuget is called lib.1.0.0.nupkg, the props file should be named as lib.props file so that it will work. It should be the same as the nuget_id.

2) add these on lib.props file:

<Project>
  <Target Name="CopyFiles" BeforeTargets="Build">
    <ItemGroup>     
      <File Include="$(MSBuildThisFileDirectory)..\Binary\*.*"></File>          
    </ItemGroup>
      <Copy SourceFiles="@(File)" DestinationFolder="$(ProjectDir)Binary"></Copy>
  </Target>
</Project>

3) add these on xxx.csproj file of the lib project.

<ItemGroup>
<None Include="xxx\**.**"(all the binary files) Pack="true" PackagePath="Binary"></None>
<None Include="build\lib.props" Pack="true" PackagePath="build"></None>
</ItemGroup>

4) It will copy the binary files into a folder called Binary into your main project when you install this new version of the nuget package.

Also, when you install this new version of nuget package, please clean your nuget caches or delete all files under C:\Users\xxx(current user)\.nuget\packages.

Besides, there is a similar issue about the steps.

这篇关于为什么将NuGet包中的内容文件添加为.NET Core项目中的链接,如何防止这种情况发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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