Could not load file or assembly System.Net.Http, Version=4.0.0.0 with ASP.NET (MVC 4) Web API OData Prerelease

asp.net Mvc-4asp.net Web-ApiOdataAssembly Resolution

asp.net Mvc-4 Problem Overview


Problem

After installing the Microsoft ASP.NET Web API OData package 5.0.0-rc1 prerelease I end up with the following exception:

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

My MVC 4 project is brand new and really small, nothing fancy in it. I target .NET framework 4.5

I need this [nuget package][1] to [implement PATCH using the Delta class][2] (When I use the version 4.0.0.0 of the package, the Delta class is not working).

How can I fix that?

My versions of System.Web.Http

In GAC I have version 5.0.0.0 of System.Web.Http

> gacutil -l System.Web.Http The Global Assembly Cache contains the > following assemblies: System.Web.Http, Version=5.0.0.0, > Culture=neutral, PublicKeyToken=31bf3856ad364e35, > processorArchitecture=MSIL

In Visual Studio, when I browse assemblies, the given version of System.Web.Http is 4.0.0.0 (Why?)

In my project, the reference to System.Web.Http

  • Has the version 5.0.0.0
  • Points to the \lib\net45\ folder of the package
  • Has CopyLocal=true

Things I tried

I tried to bind redirect v 4.0.0.0 to 5.0.0.0 in Web.config

<dependentAssembly>
	<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
	<bindingRedirect oldVersion="4.0.0.0-4.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>

But it gives me another exception:

> Attempt by method 'System.Web.Http.GlobalConfiguration..cctor()' to > access field > 'System.Web.Http.GlobalConfiguration.CS$<>9__CachedAnonymousMethodDelegate2' > failed.

I guess that v 4.0.0.0 really need to be used by core Web Api engine.

Linked questions

https://stackoverflow.com/questions/12120741/code-analysis-error-could-not-load-file-or-assembly-system-net-http-version-2 https://stackoverflow.com/questions/9431975/could-not-load-file-or-assembly-system-net-http-version-2-0-0-0-in-mvc4-web-ap

[1]: http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData/ "Microsoft Asp.NET Web Api oData" [2]: https://stackoverflow.com/questions/14177676/whats-the-currently-recommended-way-of-performing-partial-updates-with-web-api

asp.net Mvc-4 Solutions


Solution 1 - asp.net Mvc-4

Visual Studio 2013 has a new feature to take care of this. When you build the app, you should see warnings about different versions of an assembly being referenced. Double click the warning to add assembly binding redirects to the web.config.

See http://msdn.microsoft.com/en-us/library/2fc472t2.aspx for more details.

jeff.eynon notes below that you need to have the web.config checked out (if using TFS source control) to get VS to edit the file automatically. Thanks for the tip!

Solution 2 - asp.net Mvc-4

I made it work by upgrading the WebApi package to the prerelease version using nuget:

PM> Microsoft.AspNet.WebApi -Pre

In order to force the project using the latest version of WebApi, some modifications to the root Web.config were necessary:

  1. Webpages Version from 2.0.0.0 to 3.0.0.0

<appSettings>
	<add key="webpages:Version" value="3.0.0.0" />
</appSettings>

2) Binding redirect to 5.0.0.0 for System.Web.Http and System.Net.Http.Formatting

<dependentAssembly>
	<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
	<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
	<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
	<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>

I think that's it

PS: Solution highly inspired from https://stackoverflow.com/questions/18347290/webapi-odata-5-0-beta-accessing-globalconfiguration-throws-security-error/18700279#18700279

Solution 3 - asp.net Mvc-4

I experienced this issue when I tried to update a Hot Towel Project from the project template and when I created an empty project and installed HotTowel via nuget in VS 2012 as of 10/23/2013.

To fix, I updated via Nuget the Web Api Web Host and Web API packages to 5.0, the current version in NuGet at the moment (10/23/2013).

I then added the binding directs:

<dependentAssembly>
  <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
  <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>

Solution 4 - asp.net Mvc-4

I met the same problem and I resolved it by setting CopyLocal to true for the following libs:

System.Web.Http.dll
System.Web.Http.WebHost.dll
System.Net.Http.Formatting.dll

I must to add that I use MVC4 and NET 4

Solution 5 - asp.net Mvc-4

Or you could do this from NuGet Package Manager Console

 Install-Package Microsoft.AspNet.WebApi -Version 5.0.0

And then you will be able to add the reference to System.Web.Http.WebHost 5.0

Solution 6 - asp.net Mvc-4

Remove System.Web.Http and System.Net.Http.Formatting from your references and Add references back in by browsing to your bin folder (where they were copied to by nuget) Now the file version says 5.0.0.0

Solution 7 - asp.net Mvc-4

This error popped up several times on several different projects.

What I finally figured out is that when I would build, there was already a copy of the system.web.mvc binary assembly in my bin folder.

To fix this, right-click on the assembly in the list of references and select "properties". Check to see if this is the latest version by looking at the "Version" property. If it is, switch "Copy Local" to true.

This will make sure that the version referenced in your project is the version that will end up in your binaries folder.

If you still get the error, try running nuGet to get the latest version, then try the aforementioned again.

Good luck - this error is a pain!

Solution 8 - asp.net Mvc-4

I faced the same error. When I installed Unity Framework for Dependency Injection the new references of the Http and HttpFormatter has been added in my configuration. So here are the steps I followed.

I ran following command on nuGet Package Manager Console: PM> Install-Package Microsoft.ASPNet.WebAPI -pre

And added physical reference to the dll with version 5.0

Solution 9 - asp.net Mvc-4

I have faced same type of issue and followed the below steps to resolved the issue

Go to Tools --> Library Package Manager --> Package Manager Console and run the below command

Install-Package Microsoft.ASPNet.WebAPI -pre

Solution 10 - asp.net Mvc-4

i solve by way nuget. the first you install nuget. the second you use.
illustration follow:

third : Check to see if this is the latest version by looking at the "Version" property.

The finaly : you check project have latest version again.

Solution 11 - asp.net Mvc-4

After modifying the References in the Web.config file as mentioned above, we resolved the references.

I was facing similar issue.

For us we have reference Microsoft.Data.Edm.dll and OData.dll and other assemblies from Program Files:

C:\Program Files (x86)\Microsoft WCF Data Services\5.0
                          \bin\.NETFramework\Microsoft.Data.Edm.dll

and

C:\Program Files (x86)\Microsoft WCF Data Services\5.0
                        \bin\.NETFramework\Microsoft.Data.OData.dll

and version was 5.6.4.

Once I change the reference of both assemblies to C:\....Project\packages\Microsoft.Data.Edm.5.6.0 , the issue was resolved

Solution 12 - asp.net Mvc-4

Went into nuget package manager and updated my packages. Now it works. The main one I updated was the Microsoft.AspNet.WebApi.Core. May need to do this with both projects to sync up the proper references.

Solution 13 - asp.net Mvc-4

If this issue occurs, kindly check web.config in below section

Below section gives the version of particular dll used

after checking this section in web.config, open solution explorer and select reference from the project tree as shown . Solution Explorer->Reference

After expanding reference, find the dll which caused the error. Right click on the dll reference and check for version like shown in the image above.

If both config dll version and referenced dll is different you would get this exception. Make sure both are of same version which would help.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYves M.View Question on Stackoverflow
Solution 1 - asp.net Mvc-4Jay DouglassView Answer on Stackoverflow
Solution 2 - asp.net Mvc-4Yves M.View Answer on Stackoverflow
Solution 3 - asp.net Mvc-4Kevin LaBrancheView Answer on Stackoverflow
Solution 4 - asp.net Mvc-4BronekView Answer on Stackoverflow
Solution 5 - asp.net Mvc-4Sameer AlibhaiView Answer on Stackoverflow
Solution 6 - asp.net Mvc-4JJ_Coder4HireView Answer on Stackoverflow
Solution 7 - asp.net Mvc-4user1628627View Answer on Stackoverflow
Solution 8 - asp.net Mvc-4Chandra Prakash SoniView Answer on Stackoverflow
Solution 9 - asp.net Mvc-4Santu GhoshView Answer on Stackoverflow
Solution 10 - asp.net Mvc-4Nguyễn Thị NữView Answer on Stackoverflow
Solution 11 - asp.net Mvc-4Gaurav GuptaView Answer on Stackoverflow
Solution 12 - asp.net Mvc-4ExzileView Answer on Stackoverflow
Solution 13 - asp.net Mvc-4user3235808View Answer on Stackoverflow