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

其他回答

这类问题的一般答案是像其他答案一样使用绑定重定向。然而,这只是问题的一部分——您需要知道所使用的程序集文件的正确版本。Windows财产并不总是准确的,nuget也不总是准确的。

获取正确版本信息的唯一方法是分析文件本身。一个有用的工具是dotPeek。根据我的经验,dotPeek中列出的程序集名称总是准确的。

例如,此文件的正确绑定如下:

<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>

Windows资源管理器称该文件为4.6.26515.06,nuget称其为5.0.0.0文件。dotPeek说它是4.2.1.0,这是在我们的软件中正确工作的版本。还要注意,公钥和文化很重要,dotPeek也会显示这些信息。

就我而言,问题出在椅子和键盘之间:-)

Could not load file or assembly 'DotNetOpenAuth.Core, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=2780ccd10d57b246' or one of its dependencies.
The located assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)

两个或多个不同的程序集希望使用不同版本的DotNetOpenAuth库,这不是问题。此外,在我的本地计算机上,NuGet自动更新了web.config:

<dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
    </dependentAssembly>
    <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
</dependentAssembly>

然后我意识到我忘记了将新的web.config复制/部署到生产服务器。因此,如果您有手动部署web.config的方法,请检查它是否已更新。如果生产服务器的web.config完全不同,则必须在使用NuGet后同步合并这些dependentAssembly部分。

以下命令将任何程序集版本重定向到3.1.0.0版本。我们有一个脚本,它将始终更新App.config中的此引用,因此我们再也不用处理此问题了。

通过反射,您可以获取程序集publicKeyToken,并从.dll文件本身生成此块。

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <dependentAssembly>
    <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="3.1.0.0" />
  </dependentAssembly>
</assemblyBinding>

注意,如果没有XML命名空间属性(xmlns),这将无法工作。

我的问题是,在新版本中删除了旧dll的dployed。为了解决这个问题,我只选中了在发布时删除目标位置的其他文件的复选框。删除目标位置的其他文件

我的app.config包含

<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.11.0"/>

对于npgsql。不知怎么的,在用户的计算机上,我的app.exe.config丢失了。我不确定这是否是一个愚蠢的用户,安装程序故障,或是杀毒软件出了问题。替换文件解决了问题。