软件包引用版本的歧义性

15
在一个项目中,有几个引用了Ninject库并且它们有各自的版本。单元测试失败了,错误如下:
Message: System.IO.FileLoadException : 无法加载文件或程序集“Ninject, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7”或其某个依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 异常从 0x80131040 开始) ---- System.IO.FileLoadException : 无法加载文件或程序集“Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7”或其某个依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 异常从 0x80131040 开始)
1- csproj 文件
<Reference Include="Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
  <HintPath>..\packages\Ninject.3.2.2.0\lib\net45-full\Ninject.dll</HintPath>
</Reference>

2- packages.config

<package id="Ninject" version="3.2.2.0" targetFramework="net462" />

3- app.config

<dependentAssembly>
    <assemblyIdentity name="Ninject" publicKeyToken="c7192dc5380945e7" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>

4- 引用文件夹 -> 展开 -> 在Ninject上右键单击 -> 属性

Version: 3.2.0.0

5- 管理NuGet包 -> 已安装 -> 在此输入图片描述

看起来我的项目中某处引用了版本4.0.0.0和3.2.0.0,我只想引用版本3.2.2.0。

如何做到这一点?

这些引用之间有什么区别?


你的绑定重定向指定应该是4.0.0.0,那可能是需要更改的第一个地方。 - Sami Kuhmonen
我遇到了相同的问题,除了我的绑定重定向设置正确。我尝试了此线程中列出的所有解决方案,但仍然没有运气。 - Patrick95
7个回答

24

我知道这是一篇旧帖子,但我认为这个见解对于解决问题非常有用。

在你的异常中,检查正在加载哪个程序集。该程序集需要它依赖的特定版本的程序集,并在其错误版本时抛出异常。 很可能是你降级了依赖程序集。即使Nuget包和引用可能是正确的,重要的是bin文件夹里面的程序集。

检查是否在清理解决方案/项目时删除了所有程序集。如果没有,请删除所有未被删除的程序集。

对于ninject,问题很可能是Ninject.Web.Common.WebHost在清理解决方案/项目时没有被删除。该程序集没有直接引用到项目中,而是被复制到了bin文件夹中。


2
我已经尝试了很多次。我的问题与上面完全相同,所有配置文件中的版本号都是精确的。在清理解决方案后,我删除所有的 bin 和 obj 文件夹,但没有任何帮助。 - Jon Koeter
在我的情况下,nugget软件包显示了正确的版本,但当我检查bin文件夹时,ninject组件仍是旧版本,但是在删除bin文件夹中的所有内容后,它可以正常工作。 - JPCS
删除bin文件夹就是解决方案!谢谢! - Alexander
@JonKoeter 你找到解决方案了吗?我遇到了和你一样的问题。 - Patrick95
@Patrick95 是的,请看下面我的回答。 - Jon Koeter
显示剩余2条评论

6

我遇到了和你一样版本号相同的问题。

Ninject 3.2.2.0 存在一些奇怪的问题。

解决方法是保留 3.2.2 包的安装,不要改变 packages.config 文件,但将 app.config 改为 3.2.0.0。是的,没错,不是改成 3.2.2.0,而是 3.2.0.0。这正是运行时异常所抱怨的,所以我想试一试。

总结一下:

CSProj 文件:

<Reference Include="Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
  <HintPath>..\packages\Ninject.3.2.2.0\lib\net45-full\Ninject.dll</HintPath>
</Reference>

Packages.config:

<package id="Ninject" version="3.2.2.0" targetFramework="net452" />

App.config:

<dependentAssembly>
    <assemblyIdentity name="Ninject" publicKeyToken="c7192dc5380945e7" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.2.0.0" newVersion="3.2.0.0" />
</dependentAssembly>

1
这对我有用。请注意,如果您也安装了Ninject.Web.Common,您可能需要对其进行相同的更改。除了此答案中的修复之外,我还必须将newVersion从3.2.3.0更改为3.2.0.0。 - Andy Nichols

4

在我的情况下,我试图找到Ninject 3.3.3.0(而我已经安装了v3.3.4)

我检查了项目文件(.csproj)、web.configpackages.config,它们都没有问题。

  • 我从所有项目中卸载了所有的Ninject NuGet包。
  • 然后去“管理解决方案的NuGet包”并重新安装所有项目的Ninject包。

我知道这看起来很傻 ... 但是对我有用!


我有相同的情况,但这个“解决方案”对我不起作用。 "System.IO.FileNotFoundException 找不到文件或程序集“Ninject,Version=3.3.3.0,Culture=neutral,PublicKeyToken=c7192dc5380945e7”或其某个依赖项。系统找不到指定的文件。" - gemr1423

3
您需要将以下节点添加到 web.config 文件的 configuration > runtime > assemblyBinding 中:
<dependentAssembly>
    <assemblyIdentity name="Ninject" publicKeyToken="c7192dc5380945e7" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.4.0" newVersion="3.3.4.0" />
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="Ninject.Web.Common" publicKeyToken="c7192dc5380945e7" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.2.0" newVersion="3.3.2.0" />
</dependentAssembly>

Ninject & entity framework web.config changes


是的,这就是缺失的拼图!干杯! - James McCormack

2
因为我正在一个Windows服务中运行Ninject,所以我需要安装ServiceName.exe.Config文件,以便在我的服务启动时正确加载app.config信息。
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
          <assemblyIdentity name="Ninject" publicKeyToken="c7192dc5380945e7" culture="neutral" />
        <bindingRedirect oldVersion="3.3.3.0" newVersion="3.3.4.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

0
我个人的经验是删除解决方案中 bin 文件夹中的所有文件。

0
这对我有用:
安装Ninject.Web.Common.WebHost包 - 版本3.3.1
在NuGet包管理器控制台中运行上述命令。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接