.net - .NET 4.7.2 如何管理在 .NET Standard 2.0 中编译的依赖项的重定向?

标签 .net nuget dependency-management .net-standard-2.0

背景

我有一个大型、旧的单体应用程序,目前以 .NET Framework 4.7.2 为目标并使用几个最近编写的模块(也以 net472 为目标),没有任何问题。还有一个最近编写的针对 .NET Standard 2.0 的模块,它给我带来了一些问题。

所有模块都使用 Nuget 包加载到主应用程序中。

问题

当试图调用模块的主要方法时(即,在运行时,在已部署的应用程序中),它会出错,提示它找不到特定版本的 System.Diagnostics.DiagnosticSource ,尽管该 DLL 存在于相应的 \bin 文件夹中。

System.Diagnostics.DiagnosticSource 的依赖来自 Microsoft.EntityFrameworkCore.SqlServer,它通过 Microsoft.EntityFrameworkCore 依赖它和 System.Data.SqlClient

我曾怀疑从 net472 应用程序调用 EntityFrameworkCore 包这一事实可能与此有关(现在仍然可能),但是 < strong>现有的 net472 模块也使用 Core 包没有错误。 完全相同的版本,甚至!

复杂因素

  • 新模块只是一个类库,而旧模块是成熟的 WPF 应用程序。
  • 我无法将新的 Nuget 包上传到我们的服务器,因此每次进行更改时,我都必须将模块的 \bin\Debug 文件夹中的 DLL 复制到 \bin 应用程序安装目录中的文件夹。

什么没用

  • System.Diagnostics.DiagnosticSource Nuget 包安装到模块中。我已经尝试过 4.0.3.0(错误消息声称要查找的内容)、4.5.0(其他模块所期望的内容)和 4.5.1(我认为对应于安装目录中已有的文件版本,见下文).
  • 在模块的解决方案中创建一个 net472 项目以在没有 Nuget 的情况下使用 netstandard2.0 项目,然后重定向主应用程序以通过 DLL 引用使用该项目。

什么有效

  • 在安装文件夹的 \bin 目录中创建一个新文件夹,以仅包含新模块所需的 System.Diagnostics.DiagnosticSource.dll 版本,即文件版本 4.6.26515.6\bin 文件夹已包含版本 4.6.26919.2,但模块不喜欢那个版本。

不过,这是一个拼凑。我不应该仅仅因为我有一个基于 .NET Standard 构建的 Nuget 包就打包同一个 DLL 的两个版本......我应该吗?

我从这里去哪里?

附加信息

这是相关的 fuslogvw 条目。请注意,它声称正在寻找版本 4.0.3.0,尽管 MyApp.exe.config 具有指定 0.0.0.0-4.5.1 的重定向,尽管 VS 显示 Microsoft.EntityFrameworkCore 作为 System.Diagnostics.DiagnosticSource (4.5.0) 的依赖项。

*** Assembly Binder Log Entry  (10/28/2019 @ 1:22:30 PM) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\myApp\install\MyApp.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Diagnostics.DiagnosticSource, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
(Fully-specified)
LOG: Appbase = file:///C:/myApp/install/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyApp.exe
Calling assembly : Microsoft.EntityFrameworkCore, Version=2.2.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\myApp\install\MyApp.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.0.3.0 redirected to 4.5.1.0.
LOG: Post-policy reference: System.Diagnostics.DiagnosticSource, Version=4.5.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/myApp/install/System.Diagnostics.DiagnosticSource.DLL.
LOG: Attempting download of new URL file:///C:/myApp/install/System.Diagnostics.DiagnosticSource/System.Diagnostics.DiagnosticSource.DLL.
LOG: Attempting download of new URL file:///C:/myApp/install/bin/System.Diagnostics.DiagnosticSource.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\myApp\install\bin\System.Diagnostics.DiagnosticSource.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System.Diagnostics.DiagnosticSource, Version=4.0.3.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

这是 MyApp.exe.config 中的条目:

<dependentAssembly>
    <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.5.1" newVersion="4.5.1" />
</dependentAssembly>

最佳答案

答案似乎是“与任何其他重定向一样”。

fuslogvw 转储显示了确切的问题,并解释了我看到的所有其他错误。我想要合并到的 DLL(所有其他模块正在使用的 DLL,即文件版本 4.6.26919.2)不是程序集版本 4.5.1,而是 4.0.3.1.

更正 myApp.exe.config 中的绑定(bind)重定向解决了问题。

关于.net - .NET 4.7.2 如何管理在 .NET Standard 2.0 中编译的依赖项的重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593780/

相关文章:

c# - C# 中的事务

c# - 为什么在通过 NuGet 添加时,某些程序集引用有版本而另一些则没有

asp.net-core - 使用 .Net Core 安装 DotLiquid 时出现 NU1108 错误

maven - 在 maven 2 依赖树中解释 "omitted for conflict"

c# - Nuget 项目依赖 hell

javascript - 如何管理 Javascript 库中的依赖关系

c# - 复制后关闭句柄

c# - Windows Phone 8 上的 2D 游戏有哪些选项?

c# - 如何将 .NET 库移动到子目录?

c# - 与另一个项目一起开发共享库,而不让它们位于同一个解决方案中