我正在尝试在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文件的旧版本的内容,有什么建议吗?

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


当前回答

在我的案例中,上述解决方案都不起作用——问题是由app.config中定义的旧的.net配置(2.0)架构引起的,因为bindingRedirect不起作用。

删除xmlns=“http://schemas.microsoft.com/.NetConfiguration/v2.0“来自app.config或web.config,因为在较旧版本的.net framework(如2.0)中似乎不支持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生成所有必要的绑定重定向运行应用程序,看看它是否正常工作。如果没有,请添加包控制台缺少的任何缺少的绑定重定向。

没有适合我的解决方案。我尝试了清理项目解决方案、删除bin、更新包、降级包等……两个小时后,我用程序集从项目加载了默认App.config,并在那里更改了错误的引用版本:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0" />
</dependentAssembly>

to:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.14.0.0" newVersion="5.5.0.0" />
</dependentAssembly>

在这之后,我清理了这个项目,再次构建它,它成功了。没有警告没有问题。

您可以做一些事情来解决此问题。首先,使用Windows文件搜索在硬盘上搜索程序集(.dll)。一旦有了结果列表,请执行查看->选择详细信息。。。然后选中“文件版本”。这将在结果列表中显示版本号,因此您可以看到旧版本可能来自何处。

此外,正如Lars所说,检查您的GAC,看看那里列出了什么版本。这篇Microsoft文章指出,在生成过程中,在GAC中找到的程序集不会在本地复制,因此您可能需要在全部重新生成之前删除旧版本。(有关创建批处理文件以执行此操作的说明,请参见我对此问题的回答)

如果仍然无法确定旧版本的来源,可以使用Visual Studio附带的fuslogvw.exe应用程序获取有关绑定失败的详细信息。Microsoft在此处提供了有关此工具的信息。请注意,您必须通过将HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion\EnableLog注册表项设置为1来启用日志记录。

从文件夹位置手动删除旧程序集,然后将引用添加到新程序集可能会有所帮助。

.NET程序集加载器:

找不到1.2.0.203但确实找到了1.2.0.200

此程序集与请求的程序集不匹配,因此会出现此错误。

简单地说,它找不到被引用的程序集。通过将程序集放在GAC或应用程序路径中,确保它可以找到正确的程序集。

运行以下命令将程序集dll文件添加到GAC:

gacutil /i "path/to/my.dll"

另请参见https://learn.microsoft.com/archive/blogs/junfeng/the-located-assemblys-manifest-definition-with-name-xxx-dll-does-not-match-the-assembly-reference.