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

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


当前回答

我添加了一个NuGet包,结果发现我的应用程序的黑盒部分引用了旧版本的库。

我删除了包并引用了旧版本的静态DLL文件,但web.config文件从未从以下位置更新:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

恢复到卸载包时应该恢复的状态:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>

其他回答

您可能在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生成所有必要的绑定重定向运行应用程序,看看它是否正常工作。如果没有,请添加包控制台缺少的任何缺少的绑定重定向。

如果您正在使用Visual Studio,请尝试“清理解决方案”,然后重新生成项目。

清理并重建解决方案可能不会替换输出目录中的所有dll。

我建议尝试将文件夹从“bin”重命名为“oldbin”或将“obj”重命名为”oldobj“

然后再次尝试建立您的解决方案。

如果您正在使用任何第三方dll,则需要在成功构建后将其复制到新创建的“bin”或“obj”文件夹中。

希望这对你有用。

尝试从webConfig/appConfig中删除程序集引用

 <dependentAssembly>
        <assemblyIdentity name="System.IO" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.3.0.0" />
      </dependentAssembly>

如果尝试使用反射进行后期绑定,如果绑定到的程序集具有强名称或其公钥标记已更改,则会引发完全相同的错误。即使实际上没有使用指定的公钥令牌找到任何程序集,错误也是一样的。

您需要添加正确的公钥令牌(可以使用dll上的sn-T获取它)以解决错误。希望这有帮助。