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

Pascal
  • 12,265
  • 25
  • 103
  • 195

2 Answers2

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.

Alex Meyer-Gleaves
  • 3,821
  • 21
  • 11
  • Thank you Alex, good that I included my question with "autofac" tag ;-) Tested the new nuget and it works! – Pascal Jan 28 '14 at 19:05
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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • So who is to blame? Microsoft for making crap out of the System.Web.Http public key? – Pascal Jan 27 '14 at 20:45
  • 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... – Pascal Jan 27 '14 at 20:46
  • 1
    @Pascal 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. – CodeCaster Jan 27 '14 at 20:56
  • 1
    YEAH... it works thanks a bunch :) all autofac user owe you a beer :P – Pascal Jan 27 '14 at 21:12