.NET 4.8 Binding Redirect Guidance

299 Views Asked by At

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:

  1. Make sure "Auto-generate binding redirects" is enabled in project Properties
  2. 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>
  1. 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>
  1. 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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects><!-- this was enabled already in both my projects -->
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType><!--  added this manually to both project files -->

Original answer found here: https://stackoverflow.com/a/46571808/9882482

9
On

CsvHelper depends on Microsoft.Bcl.AsyncInterfaces v1.0.0

No. It depends on >= 1.0.0

enter image description here

So under the rules for NuGet package dependency resolution, this is a "Cousin Dependency" and the project will restore Microsoft.Bcl.AsyncInterfaces v7.0.0, and generate this redirect

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

Because that version is compatible with both packages.