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

3

11

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!

Summerlin answered 5/3, 2015 at 11:15 Comment(0)
S
30

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>
Summerlin answered 5/3, 2015 at 12:48 Comment(4)
I can confirm that fixed the issue for me tooToweling
this helped me as well. you should have marked this as answer.Adamson
All I had to do was update the <bindingRedirect ... /> element to what @Summerlin posted. Thanks!Epstein
If you use Nuget Package Manager, it will fix it for you when installing the WebApi package again.Their
E
1

Open managed nuget packages for solution, and install the latest Microsoft Asp.net Web Api 2.2 and that solved my problem.

Erubescence answered 2/4, 2015 at 8:28 Comment(0)
B
0

Some times, the .nuget restore could give an issue.. though after successful checkin, setting disableSourceControlIntegration to false in .nuget.config file, would resolve this issue. add key="disableSourceControlIntegration" value="false"

Breedlove answered 21/1, 2017 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.