Could not load file or assembly System.Net.Http.Primitives. Located assembly's manifest definition does not match the assembly reference

ExceptionGoogle Api-Dotnet-Client

Exception Problem Overview


I'm working on a program that uses the Google API. However every time I run my program, it I keeps getting the following error:

>Could not load file or assembly 'System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f711d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

I'm using Visual Studio 2012 express. I've tried following this link and looked through many forums, but none seem to work. The main problem seems to come from the DLL file "Google.Apis.dll" which I referenced, and it references System.Net.Http.Primitives v1.5.0.0. However the version my program references is 2.2.13.0. I've tried having the program reference v1.5.0.0 instead (I manage to find the dll along with the source code of Google.Apis) however this only caused another problem in which I needed a more recent version of System.Net.Http.Primitives.

I'm been trying find a way to work around this, however I can't seem to find anything that works. Thank you for time.

Exception Solutions


Solution 1 - Exception

I ran into the same issue with the Google API's. The main issue here is if you install the Microsoft Http Client Libraries it puts in your project an updated version of the System.Net.Http.Primitives DLL. The web.config assumes you are still using the default version of 1.5. There are two things that need to happen to fix it:

First: Update to the latest versions of Google API and Microsoft Http Client Libraries. You can install the updates via NuGet. Right click on your website, click "Manage NuGet Packages", select Updates on the left. At the time of this post some of the Google API's are prerelease only. You can install them via NuGet by selecting "include prerelease" on the top left of the update screen.

Second Update/add a dependentAssembly into your web.config. To do this you need to know the version of the System.Net.HTTP.Primitives.dll that was installed. Look in your bin directory within Windows Explorer. Find System.Net.HTTP.Primitives.dll, right click on it, select properties, and click the "Details" tab. Note the version located there. At the time of this post mine was 4.0.10.0.

Then add/update a dependentAssembly section for the correct version.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
      <bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Solution 2 - Exception

What worked for me was to simply install the "Microsoft Http Client Libraries" from Nuget.

Solution 3 - Exception

Add following to your web.config (app.config):

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
      <bindingRedirect oldVersion="0.0.0.0-4.2.13.0" newVersion="4.2.13.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Solution 4 - Exception

For me it worked the following:

On Visual Studio (2012) > Tools > Nuget Package Manager > Package Manager Console. On top of package Manager Console I have Package Source: nuget.org Default project: the project that requires the System.Net.Http.Primitives Watching inside the project file (yourproject.csproj) with an editor I read which version is needed (in my case was Microsoft.Net.Http.2.2.28)

So I went to https://www.nuget.org/packages/Microsoft.Net.Http/ and I clicked on my version under "Version History" (scroll a bit the page if you don't see it). After choosing the version you copy the suggested command - in my case it was:

Install-Package Microsoft.Net.Http -Version 2.2.28

but if you need the latest version is just this:

Install-Package Microsoft.Net.Http

and you paste it on your Visual Studio Package Manager Console previously opened as I described before. Execute the command.

In the project under the references, System.Net.Http.Primitives is now updated.

Solution 5 - Exception

I've had a similar problem.

Try to update nuget (tools/extensions and updates...) Solved that and some other problems for me.

/Jonas

Solution 6 - Exception

We were having the similar problem. But in my case, the solution of modifying the app.config by Paul didn't work. The reason is I am using it as a plugin inside another application. So it considers that application's configuration file. So we got the Google api code from GitHub and built the Google.Apis.Core library after removing the dependency of System.Net.Http.Primitives. Then we used that dll which solved our issue.

Solution 7 - Exception

I ran into this problem when I released my code that uses Google.Apis.Drive.v2 (v1.9.2.1860) to the company I work for. I gave them the exe and all of the DLLs that Visual Studio (and NuGet) generated, and they got the error. I never got the error.

The fix was easy (once I figured it out): When installing the api from Nuget the file 'assemblyname.exe.config' is automatically generated in the output (aka, Debug or Release) folder. All you have to do is include that file when you are running the assembly somewhere other than the folder it was generated. Here's the code for that file for me:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.29.0" newVersion="4.2.29.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

This is basically Paul's "second" fix but it's automatically generated by the package manager. The issue for me was when I tried his "first" fix by updating to Google.Apis.Auth and Google.Apis.Core (v1.9.3) it made things worse. I'd get the same error except now it was for "Google.Apis.Core" was the wrong version (although that probably could have been solved as well by including the same .exe.config file.)

Hope this helps someone, I know this thread is pretty old, but it's the one that a quick Google search led me to.

Edit: Forgot to mention, this is relevant to a console application targeting .NET 4.5. Some of it is probably still relevant to other .NET targets or ASP.NET but I don't know for sure. Your mileage may vary.

Solution 8 - Exception

The above answer about assemblybinding are correct, but you should NOT touch the machine.config.

The assemblybinding must be set in the config file of all of the EXECUTABLE assemblies of your project (.exe.config) who reference your library, and not in the library assemblies (.dll.config)

Solution 9 - Exception

NuGet made the following change in Web.Config

<dependentAssembly>
				<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
				<bindingRedirect oldVersion="0.0.0.0-4.2.22.0" newVersion="4.2.22.0" />
</dependentAssembly>

to

<dependentAssembly>
				<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
				<bindingRedirect oldVersion="0.0.0.0-4.2.28.0" newVersion="4.2.28.0" />
</dependentAssembly>

This was following install and subsequent removal (by version control revert) of this package https://www.nuget.org/packages/Microsoft.AspNet.WebApi.MessageHandlers.Compression/

Solution 10 - Exception

Somehow the popular answer by Paul Lemke was not working for me. The project is a webforms application that started with .net v 2.0 and has been upgraded to .net version 4.5

I updated the packages and nuget created the correct dependentAssembly/bindingRedirects.

As per some of the comments, it probably is not the best idea to change your local machine.config file.

Apprently I had an attribute in my web.config file that was causing the app to ignore the bindingRedirects.

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

I removed that xmlns attribute and it started working.

Solution 11 - Exception

Was able to resolve this pretty easily. Just opened up the Nuget Package Manager and updated the Microsoft ASP.NET Web API 2.2 Client Library package. This updated Microsoft.Net.Http to the newest version which resolved the issue with the System.Net.Http.Primitives Assembly from being able to be located. Hope this helps!

Solution 12 - Exception

In my case I was referencing the NuGet packages from a class library. NuGet fails to inform us that the class library's app.config is completely ignored and we have to manually copy its contents to the .exe's app.config.

Solution 13 - Exception

I had a similar issue with PowerShell scripts for TFS 2017. The scripts called .NET code to perform custom actions during build processes. I kept getting errors about dll version conflicts.

I was unable to resolve the issue until I implemented a hook into the AppDomain AssemblyResolve event as per this answer: https://stackoverflow.com/questions/25664372/making-binding-redirects-work-for-office-add-ins/25665319#25665319

This solution forced the process to use dlls from the current path. I know this is somewhat of a hack, but I had read that when running PowerShell, you cannot always use binding redirects, which is what I originally though I could try: https://github.com/google/google-api-dotnet-client/issues/555

Solution 14 - Exception

Install-Package Microsoft.Net.Http -Version 2.2.22

This version has that dll \packages\Microsoft.Net.Http.2.2.22\lib\net45\System.Net.Http.Extensions.dll

Solution 15 - Exception

In my case, Nuget automatic add following to web.config

<runtime xmlns="">
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.22.0" newVersion="2.2.22.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
  </dependentAssembly>
</assemblyBinding>

But I still got the above error Message, finally I figure it out. You have to copy those tags to the machine.config file located at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config.

To find the tag of "runtime" on machine.config, replace or add(if there is no such tag) it with your tags from your web.config (the ones I listed just above).

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
QuestionTriView Question on Stackoverflow
Solution 1 - ExceptionPaul LemkeView Answer on Stackoverflow
Solution 2 - ExceptionSenkweView Answer on Stackoverflow
Solution 3 - ExceptionAviwView Answer on Stackoverflow
Solution 4 - ExceptionDaniele D.View Answer on Stackoverflow
Solution 5 - ExceptionJoBeView Answer on Stackoverflow
Solution 6 - ExceptionAshishView Answer on Stackoverflow
Solution 7 - ExceptionAndrew RothView Answer on Stackoverflow
Solution 8 - ExceptionEchtelionView Answer on Stackoverflow
Solution 9 - Exceptiongb2dView Answer on Stackoverflow
Solution 10 - ExceptionVincejtlView Answer on Stackoverflow
Solution 11 - ExceptionR007View Answer on Stackoverflow
Solution 12 - ExceptionDBNView Answer on Stackoverflow
Solution 13 - ExceptionMr.ZzyzzxView Answer on Stackoverflow
Solution 14 - Exceptionuser2540680View Answer on Stackoverflow
Solution 15 - Exceptionflyhorse1999View Answer on Stackoverflow