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

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

问题描述

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

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


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

.NET 框架

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

.NET 核心

我添加到我的库项目中的二进制文件也被添加到引用项目但是作为链接也可以使用但在我的机器上 当然,因为 NuGet 包默认存储在 Windows 用户文件夹中.这会给将在不同机器上处理同一个项目的其他任何人带来问题,因为在这种情况下文件链接被破坏.


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

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

解决方案

其实这是正常现象.

你看到的 content 和 contentFiles 的行为是正常的,它们的规则是微软设计的.

请参考

注意:如果你的lib nuget叫做lib.1.0.0.nupkg,props文件应该命名为lib.props文件,以便它可以工作.它应该与 nuget_id 相同.

2)lib.props 文件中添加这些:

<目标名称=复制文件"BeforeTargets=构建"><项目组><File Include="$(MSBuildThisFileDirectory)..Binary*.*></File></项目组><Copy SourceFiles="@(File)";DestinationFolder=$(ProjectDir)Binary"></Copy></目标></项目>

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

<None Include="xxx**.**"(所有二进制文件) Pack="true";PackagePath=二进制"></None><None Include="buildlib.props";包=真"PackagePath=build"></项目组>

4) 当您安装这个新版本的 nuget 包时,它会将二进制文件复制到名为 Binary 的文件夹中.

另外,当你安装这个新版本的 nuget 包时,请清理你的 nuget 缓存 或删除 C:Usersxxx(current user).nugetpackages<下的所有文件/代码>.

此外,还有有关步骤的类似问题.

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="buildlib.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:Usersxxx(current user).nugetpackages.

Besides, there is a similar issue about the steps.

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

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