Autofac error: Could not load file or assembly 'System.Web.Http, Version=5.2.0.0,...' My project is Owin WebApi2 SelfHost

amitthk picture amitthk · Mar 5, 2015 · Viewed 14.6k times · Source

I have a simple project which uses Microsoft.AspNet.WebApi.OwinSelfHost. It is a Owin self hosted WebApi 2 project.The project is pretty much similar to:-

https://github.com/attilah/AngularJSAuthentication

The only difference I have is my Security module is in separate Assembly (everything similar to above project). I use Autofac to resolve my dependencies.

This project worked when there's no Autofac. But once I am trying to setup my project with the corresponding security modules and Autofac all I am facing is this error:-

Could not load file or assembly 'System.Web.Http, Version=5.2.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)

My configuration is very much inspired from this thread:-

Dependency injection not working with Owin self-hosted Web Api 2 and Autofac

Moreover, I found someone else struggled with this dependency bug of Autofac and was able to get similar issue resolved by re-installing Microsoft.AspNet.WebApi.WebHost:

GlobalConfiguration.Configure() not present after Web API 2 and .NET 4.5.1 migration

But this doesn't work for me I don't know why. I've tried re-installing everything from scratch also doesn't help. Besides the fact that Autofac has several permission issues while re-installing as well.

Anybody has any experience with this error? please help. Thanks!

Answer

amitthk picture amitthk · Mar 5, 2015

I managed to fix this dependency issue with help from comment by @user998660 in above thread.

What I did is:

  • I right clicked the assembly System.Web.Http in my Project's references.
  • I noticed that the version of assembly referenced by my project is 5.2.3.0
  • From the error above I know that Autofac is trying to reference System.Web.Http version 5.2.0.0. I needed a means to tell my app to use the newer version 5.2.3.0 instead of 5.2.0.0.

This is what I needed to add to my Web.config's <assemblyBinding> section:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.3.0" />
  </dependentAssembly>