I have a .NET 4.8 Framework WinForms project with the following NuGet packages:
- CsvHelper v30.0.1
- Microsoft.Extensions.DependencyInjection v7.0.0
I am encountering the following error when running unit tests:
System.IO.FileLoadException : Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
It appears that:
- CsvHelper depends on Microsoft.Bcl.AsyncInterfaces v1.0.0
- Microsoft.Extensions.DependencyInjection depends on Microsoft.Bcl.AsyncInterfaces v7.0.0
I have tried the following fixes so far which have not fixed my error:
- Make sure "Auto-generate binding redirects" is enabled in project Properties
- Binding redirect to most recent version in the App.config:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- Binding redirect to the version it wants:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="1.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- Explicitly installing Microsoft.Bcl.AsyncInterfaces v1.0.0 from NuGet. When I do this I get the following error:
Assembly 'Microsoft.Extensions.DependencyInjection' with identity 'Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' uses 'Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' which has a higher version than referenced assembly 'Microsoft.Bcl.AsyncInterfaces' with identity 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
I'm a bit lost on how to get this all wired up correctly. Has anyone run into this scenario before and was able to fix it?
EDIT
I failed to mention I was getting this error in my separate unit test project that is referencing my main project. It ended up being the main factor in this issue since the error was not occurring when debugging/executing the main project.
It appears there was an obscure setting needed in both projects in order for the auto generated binding redirect from a WinForms application to be recognized in the associated unit test project.
Original answer found here: https://stackoverflow.com/a/46571808/9882482