编程语言
首页 > 编程语言> > c#-无法从OrmLiteConfigExtensions(ServiceStack.OrmLite.Core)加载System.ComponentModel.Annotations

c#-无法从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.2.0.0,因此无法安装4.5.0.0).
我也尝试过遇到System.IO.FileLoadException时建议的various fixes,但没有任何效果.

该项目是.Net Framework 4.7.1项目,但是解决方案中包括.Net Standard项目,因此我需要运行.Stack版本的ServiceStack.OrmLite.

我已经在两个工作区(两台单独的计算机)上进行了尝试,如上所述(1)和(2)解决方案中没有.Net Standard项目的地方.
在(2)机器上,当运行非Core版本的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版本时,如果没有以下修复,我找不到避免这种情况的方法:

在我的启动项目中,我需要添加一个程序集绑定,告诉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中可以解决问题.

标签:net-core,servicestack,ormlite-servicestack,c,net
来源: https://codeday.me/bug/20191108/2006503.html