Google API nightmare: How I fixed the “Could not load file or assembly ‘System.Net.Http.Primitives, Version=1.5.0.0 …” exception

PS: This solution may or may not apply to your situation; my environment consisted of VS 2012, .NET 4.0, C#, Non-MVC web application, EPi Server CMS.

Well aren’t the Google API a joy uh? Spent way too much time figuring out how to make this shit work.

My most recent yellow screen of death happening went like this:
Could not load file or assembly ‘System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Here’s what I did to solve it:

You know how NuGet adds the binding redirects automatically to your Web.config yeah? All seems jolly right?

<!-- Snippet from web.config -->
<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.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.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.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.22.0" newVersion="2.2.22.0" />
</dependentAssembly>

But you still get that frigging error right?

Turns 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.

In my machine.config file the <runtime/> tag was empty as such just illustrated.

Simply append to or replace it with your <dependentAssembly> tags from your web.config (the ones I listed just above).

<!-- Your <runtime> tag should look something like this now -->

<runtime>
    <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.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.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.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.2.22.0" newVersion="2.2.22.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

I know it doesn’t look pretty but right now I don’t care even the tiniest bit :)

If anyone know how to fix this more elegantly then feel free to share your solution in the comments below.

This entry was posted in C#, Google, Visual Studio and tagged , , . Bookmark the permalink.
  • animaonline

    Good idea! But sometimes you don’t have access to that file, this is where this might come in handy: http://stackoverflow.com/a/25665319/767580

  • Lonewolf

    Install Google API client libraries using NuGet package, after installing the .config file gets updated with the entries and you’ll the Google.Apis….dlls referenced in the project….only thing that worked for me…

  • Shaun Mossop

    Thanks

  • Fatima Benradi

    i have the same problem but this solution dosen’t work :(

    • dererer

      C:WindowsMicrosoft.NETFramework64v4.0.30319Configmachine.config

      maybe use not 64 bit framework try correct this file

      C:WindowsMicrosoft.NETFrameworkv4.0.30319Configmachine.config

      • byte2702

        I have the same problem, this solution doesn’t work, and I have reworked both machine.config’s.

  • dererer

    excellent, thank you very much!

    I’m looking for a solution all day!

    Thanks you!

  • Pingback: Could not load file or assembly 'System.Net.Http.Primitives, Version=1.5.0.0 - CSS PHP