无法在两个NET Standard项目中整合NuGet程序包传递依赖项版本

编程入门 行业动态 更新时间:2024-10-25 15:31:51
本文介绍了无法在两个NET Standard项目中整合NuGet程序包传递依赖项版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

将EF Core添加到NET Standard项目中会引入与其他项目中的NuGet软件包不兼容的传递依赖版本

我有一个包含多个.NET Standard 2.0项目的解决方案.

I have a solution with multiple .NET Standard 2.0 projects.

一个项目A 使用 Google.Protobuf(3.11.2) NuGet软件包,该软件包取决于

One Project A uses the Google.Protobuf (3.11.2) NuGet package, that depends on

System.Memory (4.5.3) System.Buffers (4.4.0) System.Numerics.Vectors (4.4.0) System.Runtime.CompilerServices.Unsafe (4.5.2)

其他一些项目也依赖于 System.Memory 和都使用相同的依赖版本.

A few other projects also depend on System.Memory and all use the same dependency versions.

另一个项目B 使用依赖于

System.Memory (4.5.3) System.Buffers (4.5.0) System.Numerics.Vectors (4.5.0) System.Runtime.CompilerServices.Unsafe (4.7.0)

即使在两种情况下, System.Memory 版本均为(4.5.3),它仍取决于 System.Buffers , System.Numerics.Vectors 和 System.Runtime.CompilerServices.Unsafe 及其版本不同.

Even though the System.Memory version is (4.5.3) in both cases, it depends on System.Buffers, System.Numerics.Vectors and System.Runtime.CompilerServices.Unsafe and their versions differ.

当我运行使用这些项目的应用程序(使用Unity IoC的Microsoft Prism WPF .NET Framework 4.8应用程序)时,UnityContainer引发以下异常:

When I run the application that uses these projects (a Microsoft Prism WPF .NET Framework 4.8 app that uses Unity IoC) UnityContainer throws the following exception:

System.IO.FileLoadException:'无法加载文件或程序集'System.Runtime.CompilerServices.Unsafe,Version = 4.0.4.1,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一.找到的程序集的清单定义与程序集引用不匹配.

System.IO.FileLoadException: 'Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

在寻找解决方案之后,我将其添加到了我的NuGet.Config中:

After searching for a solution I added this to my NuGet.Config:

<config> <add key="DependencyVersion" value="Highest" /> </config>

在%appdata%\ Nuget 和 .sln 文件的根文件夹中.

In both, %appdata%\Nuget and in the root folder of the .sln file.

我还删除了%userprofile%\.nuget \ packages 文件夹.

然后,我从项目中删除了NuGet软件包,然后再次将其添加回去,但是它们的依赖项与以前的版本相同.

Then I removed the NuGet packages from the projects and added them back again, but their dependecies come with the same versions as before.

如果我在Visual Studio中导航到管理解决方案的NuGet软件包..."并选择合并",它只会显示未找到软件包"

If I navigate to "Manage NuGet Packages for Solution..." in Visual Studio and choose "Consolidate" it just says "No packages found"

推荐答案

我设法重现了该问题.我创建了两个新的 标准2.0项目类库.

I managed to reproduce the problem. I created two new standard 2.0 project class libraries.

我首先添加了 EF Core .在第二个步骤中,我添加了 Google protobuf .

On the first I added EF Core. On the second I added Google protobuf.

与您提到的两个版本相同.

Both same versions as you mention.

对于EF核心,我创建了一个新类,该类仅继承自 DbContext .对于Protobuf,我只是创建了一个空类,因为我对如何使用它不熟悉.我仍然可以重现该问题.

For EF core I created a new class that just inherits from DbContext. For Protobuf I just created an empty class as I am not familiar on how to use it. I was still able to replicate the problem though.

然后我创建了一个引用上述两个项目的控制台应用程序框架4.7.2 .

Then I created a console app framework 4.7.2 referencing the above two projects.

我在控制台应用程序中实例化了两个类,并出现错误System.IO.FileLoadException:'无法加载文件或程序集...

I instantiated the two classes in the Console App and got error System.IO.FileLoadException: 'Could not load file or assembly...

我去了所有三个项目,并将此行添加到 csproj .

I went to all three projects and added this line to the csproj.

< RestoreProjectStyle> PackageReference</RestoreProjectStyle>

进入物业组.

<PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <RestoreProjectStyle>PackageReference</RestoreProjectStyle> </PropertyGroup>

此后,我再次构建并运行,没有出现错误.

After that I built and ran again and no error appeared.

请让我知道您的结果.即使我的解决方案对您不起作用,我相信拥有它也是一种好习惯.

引用Oren.使用.NET Standard要求您使用PackageReference消除很多软件包"的痛苦,并正确处理传递依赖关系.尽管您可以在不使用PackageReference的情况下使用.NET Standard,但我不建议您这样做."

To quote Oren. "Using .NET Standard requires you to use PackageReference to eliminate the pain of "lots of packages" as well as properly handle transitive dependencies. While you may be able to use .NET Standard without PackageReference, I wouldn’t recommend it."

汉瑟曼(Hanselman)也提到:完整"框架项目使用的是较旧的.csproj格式,默认情况下,它们使用package.config来管理依赖项.较新的项目可以将Packages作为一流的参考.因此,我们需要告诉该解决方案中的所有项目,以"PackageReferences"的身份管理和还原其软件包.

Also Hanselman mentions: "The "full" Framework projects are using the older .csproj format and by default, they use package.config to manage dependencies. The newer projects can reference Packages as first-class references. So we need to tell ALL projects in this solution to manage and restore their packages as "PackageReferences.""

这是我的消息来源.

www.hanselman/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx

oren.codes/2017/04/23/using-xamarin-forms-with-net-standard-vs-2017-edition/

根据Sommen的其他信息从 github问题更新感谢 Sommen ,以提供此额外信息.也要 Immo Landwerth 致以在GitHub提供此信息的荣誉.我将提供Github页面中已经存在的变通方法,仅作为OP jinjinov建议的功能.

Updated according to Sommen's extra info from the github issues Kudos to Sommen for providing this extra info. Also Kudos to Immo Landwerth for providing this Info at GitHub. I will provide as is the Workarounds that already exist in the Github page just for complecity as advised by the OP jinjinov.

来自 GitHub问题

  • 启用根.NET Framework应用程序中的自动绑定重定向
  • 确保您的根应用程序项目未使用 packages.config ,但对NuGet软件包使用了 PackageReference :
    • 如果您当前没有 packages.config ,只需添加< RestoreProjectStyle> PackageReference</RestoreProjectStyle> .
    • 如果您当前确实具有 packages.config ,请将内容转换为项目文件中的程序包引用.语法如下:< PackageReference Include ="package-id";版本="package-version"/>
    • Enable automatic binding redirects in the root .NET Framework application
    • Make sure your root application project doesn't use packages.config but uses PackageReference for NuGet packages:
      • If you currently don't have packages.config, simply add <RestoreProjectStyle>PackageReference</RestoreProjectStyle>.
      • If you currently do have a packages.config, convert the contents to package references in the project file. The syntax is like this: <PackageReference Include="package-id" Version="package-version" />
      • ASP.NET Web应用程序和网站

      • Web应用程序和网站不支持自动绑定重定向生成.为了解决绑定冲突,您需要双击错误列表中的警告,Visual Studio会将其添加到您的web.config文件中.
      • 在Web应用程序项目中,应启用如上所述的PackageReference.在网站上,您不能使用PackageReference,因为没有项目文件.在这种情况下,您需要将所有直接或间接项目引用所依赖的所有NuGet软件包安装到您的网站中.
      • 单元测试项目

        默认情况下,绑定重定向不会添加到类库项目中.这对于单元测试项目是有问题的,因为它们本质上就像应用程序.因此,除了自动绑定重定向,您还需要指定 GenerateBindingRedirectsOutputType :

        <PropertyGroup> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup>

        还有一个讨论部分,可提供更多信息-> GitHub讨论

        There is also a discussion section that provides more information -> GitHub discussion

  • 更多推荐

    无法在两个NET Standard项目中整合NuGet程序包传递依赖项版本

    本文发布于:2023-11-16 21:03:09,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1607445.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:两个   版本   项目   程序包   Standard

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!