Search code examples
c#.net-assemblyautofacassemblybinding

Force a 3rd party assembly to use another version of another assembly


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


Solution

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