我正在尝试在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文件的旧版本的内容,有什么建议吗?
此外,我想我的硬盘上甚至没有这个旧组件。是否有任何工具可以搜索此旧版本的程序集?
这个问题由来已久,我最近在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版本)将具有相同的版本,问题得到解决。
我也有类似的问题,但没有一个答案对我有效。
对我有效的解决方案是手动从项目文件(YourProject.csproj)中删除publicKeyToken部分。
以前是:
<Reference Include="Utility, Version=0.0.0.0, Culture=neutral, PublicKeyToken=e71b9933bfee3534, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>dlls\Utility.dll</HintPath>
</Reference>
更改后为:
<Reference Include="Utility, Version=1.0.1.100, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>dlls\Utility.dll</HintPath>
</Reference>
确保SpecificVersion为False。
您可能在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>
在这之后,我清理了这个项目,再次构建它,它成功了。没有警告没有问题。