我正在尝试在C#Windows窗体应用程序(Visual Studio 2005)中运行一些单元测试,但出现以下错误:

System.IO.FileLoadException:未能加载文件或程序集“Utility,Version=1.2.0.200,Culture=neutral,PublicKeyToken=764d581291d764f7”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT的异常:0x80131040)**位于x.Foo.FooGO()位于Foo.cs:line 123中的x.Foo.Foo2(String groupName_)位于FooTests.cs:line 98中的x.Foo.UnitTests.FooTests.TestFoo()**System.IO.FileLoadException:未能加载文件或程序集“Utility,Version=1.2.0.203,Culture=neutral,PublicKeyToken=764d581291d764f7”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT的异常:0x80131040)

我查阅了我的参考资料,我只参考了实用程序版本1.2.0.203(另一个是旧版本)。

关于我如何找出试图引用此DLL文件的旧版本的内容,有什么建议吗?

此外,我想我的硬盘上甚至没有这个旧组件。是否有任何工具可以搜索此旧版本的程序集?


当前回答

您可能在assemblyBinding中有错误的掘金版本,请尝试:

删除web.config/app.config中的所有程序集绑定内容:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.1.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.1.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
  </dependentAssembly>
</assemblyBinding>

在包管理器控制台中键入:添加BindingRedirect生成所有必要的绑定重定向运行应用程序,看看它是否正常工作。如果没有,请添加包控制台缺少的任何缺少的绑定重定向。

其他回答

您可能在assemblyBinding中有错误的掘金版本,请尝试:

删除web.config/app.config中的所有程序集绑定内容:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.1.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.1.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
  </dependentAssembly>
</assemblyBinding>

在包管理器控制台中键入:添加BindingRedirect生成所有必要的绑定重定向运行应用程序,看看它是否正常工作。如果没有,请添加包控制台缺少的任何缺少的绑定重定向。

我会让别人从我的愚蠢中受益。我对一个完全独立的应用程序有一些依赖性(让我们称之为App1)。来自App1的dll被拉入我的新应用程序(App2)。每当我在APP1中进行更新时,我都必须创建新的dll并将其复制到App2中。好我厌倦了在两个不同的App1版本之间复制和粘贴,所以我只是在dll中添加了一个“NEW_”前缀。

好我猜构建过程会扫描/bin文件夹,当它不正确地匹配某个内容时,会弹出与上面提到的相同的错误消息。我删除了我的“新版本”,它做得很好。

在我的案例中,在运行ASP.NET应用程序时发生了此错误。解决方案是:

删除项目文件夹中的obj和bin文件夹

清理不起作用,重建不起作用。所有引用都很好,但它没有编写其中一个库。删除这些目录后,一切都很顺利。

我也有同样的错误,但在我的案例中,我在本地运行一个自定义的nuget包到另一个项目中。

为我修复的是将包版本更改为错误中请求的版本。

该包在netstandard 2.1中,请求该包的项目在netcore 3.1中

如果您遇到类似“找到的程序集的清单定义与程序集引用不匹配”的错误,并且您已经通过VS中的“项目”>“管理NuGet程序包和更新”选项卡进行了更新,那么您可以做的第一件事就是在从NuGet Gallery页面检查版本并从package Manager控制台运行以下命令后,尝试安装另一个版本的程序包:

PM> Install-Package YourPackageName -Version YourVersionNumber 
//Example
PM> Install-Package Microsoft.Extensions.FileProviders.Physical -Version 2.1.0

虽然答案和所讨论的包并没有直接关系,而且早在很久以前就有人问过它,但它是一种通用的,仍然相关,希望它能帮助一些人。