Could not load assembly during runtime although binding redirect is correctly set

3

I have a unit test project .NET 4.6.1 where some tests fail prolly because I added a new project as a new reference to the unit test project.

This is the error I am getting:

Error: System.IO.FileLoadException: 
Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.1.0, 
Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies.
 The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).

Then I added this configuration to the app.config of the unit test project:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

But the test still fails with SAME error messsage.

Why did the binding redirect not help?

I have of course also these props in the unit test .csproj file:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

When I check the generated unit.test.config file in the debug folder it does contain many binding redirects BUT not the one why my tests fail...

Just discovered this in the output box after rebuild solution:

NU1605: Detected package downgrade: Microsoft.Extensions.DependencyInjection from 2.2.0 to 1.1.1. Reference the package directly from the project to select a different version.

 UnitTests -> MyNewReferencedProject -> Microsoft.ApplicationInsights.WorkerService 2.14.0 -> Microsoft.Extensions.DependencyInjection (>= 2.2.0) 

 UnitTests -> Microsoft.Extensions.DependencyInjection (>= 1.1.1)

Is this "warning" related to my problem in any way because my problem is about assembly Microsoft.Extensions.DependencyInjection.Abstractions

and not about Microsoft.Extensions.DependencyInjection

I have these assembly versions of the Microsoft.Extensions.DependencyInjection.Abstractons.dll in all debug\bin folders of my solution projects.

It seems I have 3 different versions floating around...

enter image description here

This is really funky... when I add now this binding redirect:

 <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>

The former runtime exception is gone because I get a new one.

It seems I fixed it but... this is wonky tonky stuff. Random shit...

Why was it fixed using 2.1.0.0 ???

@Mark Gravell

There is no inner exception its NULL.

Thats the message:

Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

odd is my tests are not really executed I mean I can not debug them. They just start and immediatelly I get this exception face slammed!

c#
.net
visual-studio
unit-testing
assembly-binding-redirect
asked on Stack Overflow Jul 20, 2020 by HelloWorld • edited Jul 21, 2020 by Kyle Wang - MSFT

1 Answer

0

That seemed to have fixed it but its not logical to me.

 <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
answered on Stack Overflow Jul 21, 2020 by HelloWorld

User contributions licensed under CC BY-SA 3.0