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

57,825

Solution 1

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

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

Solution 3

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

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

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.

Share:
57,825
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • CodingIntrigue
    CodingIntrigue over 10 years
    +1 First step was enough to resolve the issue for me
  • WhoAmI
    WhoAmI about 10 years
    What namespaces did you use after installing? Thx
  • keithl8041
    keithl8041 almost 10 years
    This worked for me when I added it specifically to my test project (where I was running in to the issue), regardless of the fact that it was already referenced in the main project which was in turn referenced by my test project.
  • Raphael Isidro
    Raphael Isidro almost 10 years
    Just to point out Http.Primitives version is "2.2.22.0" and the example code shows "4.0.10.0". But this is the right answer, should be accepted
  • jaminto
    jaminto over 9 years
    And to do this, i had to update nuget.exe in our solution - seirer.net/blog/2014/5/20/…
  • DFTR
    DFTR over 9 years
    The point of Nuget is to not require manual assembly binding in the Web.Config. I miss the old Google API already. Now If I distribute my wrapper DLL, I have to somehow notify them to do your two steps for any project that wants to use it.
  • kstubs
    kstubs almost 9 years
    Not sure about your oldVersion range value and newVersion value, my newVersion value is: 2.2.29.0
  • David Hogue
    David Hogue almost 9 years
    Thanks! This fixed an error "Method 'MyMethod' in type 'MyClass' from assembly 'MyAssembly' does not have an implementation." that only appeared when inheriting from a type in Google.Apis.Auth.Mvc4.dll. All I had to do was copy the bindingRedirect already in my app.config to my nunit config file.
  • S. Baggy
    S. Baggy almost 9 years
    4.2.29 for me, targeting .Net 4.5
  • Vincejtl
    Vincejtl almost 9 years
    @flyhorse1999, about a year late but try the solution I posted. My application did not listen to the bindingRedirects due to an attribute I needed to remove.
  • eeee
    eeee over 8 years
    In my case the vesrion nuget created was pointing to 4.2.29 but the version I had on my machine was actually 4.2.13 (GAC_MSIL\System.Net.Http.Primitives) ... when I changed the redirect to 4.2.13, it started working! Not sure why it happened but if anyone trying all kinds of workaround and still not able to make it work, it's worth a check at your GAC and see what version you actually have.
  • Der_Meister
    Der_Meister over 8 years
    Install-Package Microsoft.Net.Http
  • dakab
    dakab about 8 years
    Where do you find the library’s app.config? Did you copy all its contents?
  • DBN
    DBN about 8 years
    It is normally in the root of the project folder and is copied to bin\Foo.dll.config during compile. They are XML format so one usually can't just copy append it verbatim. They need to be compared and the missing parts individually copied.
  • rob
    rob almost 7 years
    Niether Microsoft.Net.Http nor System.Net.Http nuget packages include the System.Net.Http.Primitives.dll. Not sure if they did in the past. My search continues.
  • Kevin G
    Kevin G about 4 years
    Sorry to bump this, but this actually was the only fix for me. It explains why it would run within the debug map/debug mode and not in the released folder. Is there any way to prevent this though? Alot of info is shown there for easy grabs, wich normal users aren't suppose to access easily.
  • needfulthing
    needfulthing about 4 years
    Update with Nuget (first step) also solved the problem for me.