Force a 3rd party assembly to use another version of another assembly
Asked Answered
C

2

8

I am running integration tests and when I reach that line of code:

        WebApiDependencyResolverConfig.Register(config); 

(uses the autofac container inside)

I get this exception:

{"Could not load file or assembly 'System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"}

Fusionlog:

=== Pre-bind state information ===
LOG: DisplayName = System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/TLP/TLP.API.IntegrationTests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : Autofac.Integration.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\TLP\TLP.API.IntegrationTests\bin\Debug\TLP.API.IntegrationTests.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config

It seems the autofac web api integration works only up to web api 2.0. When I use the web api 2.1 which does not reference the system.web.http 5.0.0 anymore but instead the 5.1.0 then it does not work anymore.

How can I tell the autofac to use the system.web.http 5.1.0 version and not 5.0.0 ?

I put this in the app.config of my integration test AND API project:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Http"
                        publicKeyToken="32ab4ba45e0a69a1"
                        culture="neutral" />
      <bindingRedirect oldVersion="5.0.0.0"
                       newVersion="5.1.0.0"/>
    </dependentAssembly>
  </assemblyBinding>
  </runtime>

But it did NOT work!

Another thing that is very odd is that I have read that using .NET 4.5.1 this redirection assembly stuff is done automatically. But its not happening...

Chest answered 27/1, 2014 at 20:14 Comment(0)
D
4

As explained here, here and here, you have a Public Key Token mismatch, caused when a referenced assembly is recompiled with a different strong-named key.

This cannot be solved other than by compiling the autofac library against the newer assembly.

Dextrous answered 27/1, 2014 at 20:29 Comment(4)
So who is to blame? Microsoft for making crap out of the System.Web.Http public key?Chest
About your last sentence: Should this not be the task of the autofac author? I ask this because I would like to have this fix as a nuget package. Not some custom added reference by me...Chest
@Chest I've verified the publicKeyTokens for Web API 2 and 2.1 match. Can you try changing 32ab4ba45e0a69a1 to 31bf3856ad364e35 in your config? If the token would have changed, then yes, the autofac folks should release an update, but it looks like this isn't the case.Dextrous
YEAH... it works thanks a bunch :) all autofac user owe you a beer :PChest
E
8

I have pushed updated packages to NuGet for Web API 2.1 and MVC 5.1.

http://www.nuget.org/packages/Autofac.WebApi2

https://www.nuget.org/packages/Autofac.Mvc5

Microsoft obviously interprets the rules of semantic versioning differently, because 5.1 which is a minor version, should "add functionality in a backwards-compatible manner". This is not the case with the 5.1.0 packages as the strong named assembly versions were increased.

The Autofac assemblies are strong named, but during the 3.0 series of versions, only the package and file versions are updated. The assembly version always remains at 3.0.0.0 to prevent this sort of breaking change during package updates. Unfortunately, we have no control over the ASP.NET dependencies and have to recompile when something like this happens.

Erroneous answered 28/1, 2014 at 13:0 Comment(1)
Thank you Alex, good that I included my question with "autofac" tag ;-) Tested the new nuget and it works!Chest
D
4

As explained here, here and here, you have a Public Key Token mismatch, caused when a referenced assembly is recompiled with a different strong-named key.

This cannot be solved other than by compiling the autofac library against the newer assembly.

Dextrous answered 27/1, 2014 at 20:29 Comment(4)
So who is to blame? Microsoft for making crap out of the System.Web.Http public key?Chest
About your last sentence: Should this not be the task of the autofac author? I ask this because I would like to have this fix as a nuget package. Not some custom added reference by me...Chest
@Chest I've verified the publicKeyTokens for Web API 2 and 2.1 match. Can you try changing 32ab4ba45e0a69a1 to 31bf3856ad364e35 in your config? If the token would have changed, then yes, the autofac folks should release an update, but it looks like this isn't the case.Dextrous
YEAH... it works thanks a bunch :) all autofac user owe you a beer :PChest

© 2022 - 2024 — McMap. All rights reserved.