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

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


当前回答

我只是找到了另一个原因,为什么会出现这个错误。我从特定库的所有版本中清理了我的GAC,并参照与可执行文件一起部署的特定版本构建了我的项目。当我运行这个项目时,我在搜索库的更新版本时遇到了这个异常。

原因是发布者策略。当我从GAC卸载库的版本时,我忘了卸载发布者策略程序集,所以程序集加载器在GAC中找到发布者策略,告诉它搜索新版本,而不是使用本地部署的程序集。

其他回答

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

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部分。

这是我解决这个问题的方法。

从异常消息中,获取“问题”库的名称和“预期”版本号。

在解决方案中查找该.dll的所有副本,右键单击它们,然后检查它是哪个版本的.dll。

好的,在这个例子中,我的.dll肯定是2.0.5022.0(所以异常版本号是错误的)。

在解决方案中的所有.csproj文件中搜索异常消息中显示的版本号。用dll中的实际版本号替换此版本号。

所以,在这个例子中,我将替换这个。。。

<Reference Include="DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

…用这个。。。

<Reference Include="DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

工作完成!

这个问题由来已久,我最近在Azure DevOps Yaml管道和Dotnet Core 3.1中收到了同样的错误消息。这个问题与其他答案试图解决的问题有些不同,因此我将分享我的解决方案。

我有一个解决方案,为我自己的nuget包提供了许多项目。我无意中在*.csproj文件中添加了版本标记,如下所示:

  <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
  </PropertyGroup>

我用一个DotnetCoreCLI@2任务:

 - task: DotNetCoreCLI@2
   displayName: 'pack'
   inputs:
     command: pack
     nobuild: true
     configurationToPack: 'Release'
     includesource: true
     includesymbols: true
     packagesToPack: 'MyNugetProject1.csproj;**/MyNugetProject2.csproj'
     versioningScheme: 'byEnvVar'
     versionEnvVar: 'GitVersion.SemVer'

问题是*.csproj文件中的版本与环境变量GitVersion.SemVer(由输入“versionEnvVar”指定)中的版本不匹配。

删除*.csproj文件中的所有<Version>1.0.0</Version>-标记后,dll的程序集/fileversion由环境变量自动分配,nuget和dll(程序集/file版本)将具有相同的版本,问题得到解决。

我现在要让所有人都大吃一惊。

从.config文件中删除所有<assemblyBinding>引用,然后从NuGet Package Manager控制台运行以下命令:

Get-Project -All | Add-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>

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