首页 > 解决方案 > 无法从 OrmLiteConfigExtensions (ServiceStack.OrmLite.Core) 加载 System.ComponentModel.Annotations

问题描述

使用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 时建议的各种修复,但没有任何效果。

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

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

有没有人有任何想法?

标签: c#.net.net-coreservicestackormlite-servicestack

解决方案


终于明白了……</p>

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

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

在我的启动项目中,我需要添加一个程序集绑定,告诉ServiceStack.OrmLite.MySql.Core版本System.ComponentModel.Annotations4.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 将起到作用。


推荐阅读