Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 844
Pascal Avatar asked Jan 27 '14 20:01

Pascal


2 Answers

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.

like image 123
Alex Meyer-Gleaves Avatar answered Sep 20 '22 12:09

Alex Meyer-Gleaves


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.

like image 22
CodeCaster Avatar answered Sep 22 '22 12:09

CodeCaster