title: Issues with System.Net.Http.dll
body: <p>I'm having issues referring to System.Net.Http.dll version 4.2.0.0</p>
  
  <p>Here is my setup - </p>
  
  <blockquote>
    <p>Visual Studio 2017, 15.8.7</p>
    
    <p>A simple console application targeting .net 4.7.2</p>
  </blockquote>
  
  <p>When I create a new project, by default it has a reference to <code>System.Net.Http.dll</code> and the location is <code>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll</code>.</p>
  
  <p>But when I execute my code, </p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var assembly = GetAssemblyNameContainingType(typeof(HttpClient).FullName);
          Console.WriteLine("Path: " + assembly.Location);
          Console.WriteLine("Version: " + assembly.GetName().Version);
  
          Console.ReadKey();
      }
  
      public static Assembly GetAssemblyNameContainingType(String typeName)
      {
          foreach (Assembly currentassembly in AppDomain.CurrentDomain.GetAssemblies())
          {
              Type t = currentassembly.GetType(typeName, false, true);
              if (t != null)
              { return currentassembly; }
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p>I get the below output</p>
  
  <pre><code>  Path: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll
    Version: 4.0.0.0 
  </code></pre>
  
  <p>It seems to pick up reference to <code>System.Net.Http.dll</code> from GAC instead of the <code>Programs Files (x86)</code> folder. </p>
  
  <p>I specifically need reference to 4.2.0.0 - since it addresses serialization of <code>HttpRequestException</code> in a way Newtonsoft json can serialize and deserialize it without erroring out.</p>
  
  <p>I read quite a few posts about others facing an issue with <code>System.Net.Http.dll</code>, many of those said that this was addressed in Visual Studio 2017, 15.8 and that one won't have to add reference to the <code>System.Net.Http</code> nugets and should let visual studio to refer it appropriately from framework.</p>
  
  <p>Am i missing something?
  I have tried numerous things - </p>
  
  <p>Adding reference path (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2) in project properties - did not help</p>
  
  <p>Adding reference using path to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Net.Http.dll - did not help</p>
  
  <p>Copy local - True - did not help</p>
  
  <p>Specific version - True - did not help</p>
  
  <p>Added binding redirect - did not help. Gave below exception</p>
  
  <pre><code>  &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
  </code></pre>
  
  <p>System.BadImageFormatException: 'Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)'</p>
  
  <p>Unchecked Auto-generate binding redirects in project properties - did not help</p>
  
  <p>Unchecked Auto-generate binding redirects with manual binding redirect - did not help</p>
  
  <p>Unchecked Auto-generate binding redirects with manual binding redirect + copy local - did not help</p>
  
  <p>Adding a hint path - did not help
      
          C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Net.Http.dll
      </p>
  
  <p>Here is my .csproj (original, reverted after doing all above changes)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /&gt;
    &lt;PropertyGroup&gt;
      &lt;Configuration Condition=" '$(Configuration)' == '' "&gt;Debug&lt;/Configuration&gt;
      &lt;Platform Condition=" '$(Platform)' == '' "&gt;AnyCPU&lt;/Platform&gt;
      &lt;ProjectGuid&gt;{EEE1B52B-0316-4EB5-9F3B-383E377964BE}&lt;/ProjectGuid&gt;
      &lt;OutputType&gt;Exe&lt;/OutputType&gt;
      &lt;RootNamespace&gt;SysNetHttpTests&lt;/RootNamespace&gt;
      &lt;AssemblyName&gt;SysNetHttpTests&lt;/AssemblyName&gt;
      &lt;TargetFrameworkVersion&gt;v4.7.2&lt;/TargetFrameworkVersion&gt;
      &lt;FileAlignment&gt;512&lt;/FileAlignment&gt;
      &lt;AutoGenerateBindingRedirects&gt;true&lt;/AutoGenerateBindingRedirects&gt;
      &lt;Deterministic&gt;true&lt;/Deterministic&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "&gt;
      &lt;PlatformTarget&gt;AnyCPU&lt;/PlatformTarget&gt;
      &lt;DebugSymbols&gt;true&lt;/DebugSymbols&gt;
      &lt;DebugType&gt;full&lt;/DebugType&gt;
      &lt;Optimize&gt;false&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\Debug\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;DEBUG;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "&gt;
      &lt;PlatformTarget&gt;AnyCPU&lt;/PlatformTarget&gt;
      &lt;DebugType&gt;pdbonly&lt;/DebugType&gt;
      &lt;Optimize&gt;true&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\Release\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"&gt;
        &lt;HintPath&gt;..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll&lt;/HintPath&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="System" /&gt;
      &lt;Reference Include="System.Core" /&gt;
      &lt;Reference Include="System.Xml.Linq" /&gt;
      &lt;Reference Include="System.Data.DataSetExtensions" /&gt;
      &lt;Reference Include="Microsoft.CSharp" /&gt;
      &lt;Reference Include="System.Data" /&gt;
      &lt;Reference Include="System.Net.Http" /&gt;
      &lt;Reference Include="System.Xml" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Compile Include="Program.cs" /&gt;
      &lt;Compile Include="Properties\AssemblyInfo.cs" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;None Include="App.config" /&gt;
      &lt;None Include="packages.config" /&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /&gt;
  &lt;/Project&gt;
  </code></pre>