使用 ServiceStack.OrmLite.Core 包 (5.4.1) 并尝试通过执行以下操作获取 ModelDefinition ( ServiceStack.OrmLite.ModelDefinition ) 时出现运行时错误:

var model = ModelDefinition<T>.Definition;

错误如下:
System.IO.FileLoadException: 'Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

我试图安装 nuget System.ComponentModel.Annotations(4.5.0.0,因为 4.2.0.0 不可用)无济于事。
在遇到 System.IO.FileLoadException 时,我也尝试过 various fixes 建议,但没有任何效果。

该项目是一个 .Net Framework 4.7.1 项目,但 .Net Standard 项目包含在解决方案中,因此我需要运行 ServiceStack.OrmLite 的 .Core 版本。

我已经在两个工作区(两台单独的机器)上尝试过这个,(1) 如上所述,(2) 解决方案中没有 .Net Standard 项目。
在 (2) 机器上,它在运行非核心版本的 ServiceStack.OrmLite 时工作,但切换到 ServiceStack.OrmLite.Core 时发生运行时错误。

有没有人有任何想法?

最佳答案

最后我想通了……

事实证明,ServiceStack.OrmLite.MySql.Core 需要 System.ComponentModel.Annotations 版本 4.2.0.0,因为错误消息明确指出。通常它可以安装等于或大于所需的版本,但在这种情况下,这些都不起作用。出于某种原因,FX 中包含的最新版本似乎是 4.0.0.0(或者至少这是我包含它的汇编版本时得到的。)还有一个 nuget 安装版本 4.5.0.0,但结果是相同。

我不知道为什么它使用 ServiceStack.OrmLite.MySql 的 FX 版本开箱即用,但是当像我一样使用 Core 版本时,我发现没有以下修复方法就无法避免这种情况:

在我的启动项目中,我需要添加一个程序集绑定(bind),告诉 ServiceStack.OrmLite.MySql.Core System.ComponentModel.Annotations 版本 4.2.0.0 “重定向”到 4.0.0.0(同一程序集)。它看起来像这样:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.0.0.0" />
     </dependentAssembly>
 </assemblyBinding>

我敢肯定有人可以解释这方面的细节,我不是其中之一,但尽管如此……将其添加到启动项目 app.config 中就可以解决问题。

关于c# - 无法从 OrmLiteConfigExtensions (ServiceStack.OrmLite.Core) 加载 System.ComponentModel.Annotations,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54762932/

10-12 22:33