提问人:Dommicentl 提问时间:11/5/2021 更新时间:11/5/2021 访问量:274
为什么强制降级会导致 .Net Core 中的程序集加载异常?
Why does a force downgrade causes an assembly load exception in .Net Core?
问:
我有一个带有控制台和库项目的示例解决方案。两者都引用相同的 nuget,但版本不同。控制台项目还具有对库项目的引用。所以结构是这样的:
- Solution
- ConsoleApp
- Project Reference: Library
- Nuget: NServiceBus.RabbitMQ (5.2.0)
- Library
- Nuget: NServiceBus.RabbitMQ (6.0.0)
您可以在此处找到解决方案。
由于 Nuget 使用最接近的获胜规则,因此解析的 nuget 包是版本 5.2.0。这就是我想要的,到目前为止一切顺利。但是当我运行应用程序并运行库的方法时,我得到以下异常:
Could not load file or assembly 'NServiceBus.Transport.RabbitMQ, Version=6.0.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
在.NET Framework中,我将通过程序集重定向来解决此问题。但这在 .Net Core 中不可用。我一直认为 .Net Core 通过使用 deps.json 文件自动解决了这个问题。在那里,我看到以下声明:
"Library/1.0.0": {
"dependencies": {
"NServiceBus.RabbitMQ": "5.2.0"
},
"runtime": {
"Library.dll": {}
}
}
但仍然在运行时,他试图解析 6.0.0 版本。我正在使用最新的.Dot Net 3.1.X SDK。
我是做错了什么,还是这看起来像一个错误?
郑重声明,这是一个简单的示例项目。我需要这个的实际情况要复杂得多。我也明白这样做可能会导致运行应用程序时出现运行时异常。
答:
这似乎是设计使然。
经过一番搜索,我找到了这个: https://github.com/dotnet/fsharp/issues/3408#issuecomment-319466999
coreclr 将加载版本或高于引用的程序集。如果发现的程序集低于引用,则该程序集将失败。
还有这个:https://github.com/dotnet/sdk/issues/384#issuecomment-260457776
.NET Core 不支持降级程序集版本
因此,为了确认,我花了比我预期的更多的时间来查找/搜索 https://github.com/dotnet/runtime。最终,我找到了汇编版本兼容方法:https://github.com/dotnet/runtime/blob/172059af6623d04fa0468ec286ab2c240409abe3/src/coreclr/binder/assemblybindercommon.cpp#L49-L53
它分别检查版本的所有组件,但如果我们只看一个组件,我们可以看到它在做什么:
if (!pFoundVersion->HasMajor() || pRequestedVersion->GetMajor() > pFoundVersion->GetMajor())
{
// - A specific requested version component does not match an unspecified value for the same component in
// the found version, regardless of lesser-order version components
// - Or, the requested version is greater than the found version
return false;
}
正如注释所说,如果程序集的版本低于请求的版本,加载程序将拒绝该程序集。在您的例子中,假设程序集版本与包版本匹配(它不必匹配),您的库请求版本 6.0.0,但程序集加载程序/绑定器在磁盘上找到版本 5.2.0,该版本较低。因此,它拒绝该 dll,继续查找,但随后在探测路径上找不到合适的程序集版本,并最终引发 FileLoadException。
我不清楚的是,是否仅在默认程序集加载程序上检查此程序集兼容性,或者即使您将自己的事件处理程序添加到 AssemblyLoadContext。默认。正在解决。您可以尝试添加自己的处理程序,当它请求较高版本的程序集时,您仍然返回较低版本的程序集。这可能是解决该问题的一种方式。
评论