Issues with System.Net.Http.dll

0

I'm having issues referring to System.Net.Http.dll version 4.2.0.0

Here is my setup -

Visual Studio 2017, 15.8.7

A simple console application targeting .net 4.7.2

When I create a new project, by default it has a reference to System.Net.Http.dll and the location is C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll.

But when I execute my 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;
    }
}

I get the below output

  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 

It seems to pick up reference to System.Net.Http.dll from GAC instead of the Programs Files (x86) folder.

I specifically need reference to 4.2.0.0 - since it addresses serialization of HttpRequestException in a way Newtonsoft json can serialize and deserialize it without erroring out.

I read quite a few posts about others facing an issue with System.Net.Http.dll, 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 System.Net.Http nugets and should let visual studio to refer it appropriately from framework.

Am i missing something? I have tried numerous things -

Adding reference path (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2) in project properties - did not help

Adding reference using path to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Net.Http.dll - did not help

Copy local - True - did not help

Specific version - True - did not help

Added binding redirect - did not help. Gave below exception

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

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)'

Unchecked Auto-generate binding redirects in project properties - did not help

Unchecked Auto-generate binding redirects with manual binding redirect - did not help

Unchecked Auto-generate binding redirects with manual binding redirect + copy local - did not help

Adding a hint path - did not help C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Net.Http.dll

Here is my .csproj (original, reverted after doing all above changes)

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{EEE1B52B-0316-4EB5-9F3B-383E377964BE}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>SysNetHttpTests</RootNamespace>
    <AssemblyName>SysNetHttpTests</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
    <None Include="packages.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
c#
.net
http
json.net
asked on Stack Overflow Oct 25, 2018 by perf-guru • edited Oct 25, 2018 by perf-guru

1 Answer

0

Official MS answer (by Jose Perez Rodriguez):

This is by design. Due to a Windows requirement, all of the framework assemblies installed in the GAC need to be versioned 4.0.0.0. The actual implementation contained in that assembly is not the 4.0.0.0 implementation but the 4.2.0.0 instead. There are a bunch of complicated reasons of why Windows has this requirement on our assemblies installed on the GAC, like servicing, bugfixing, and just having one GAC for all .NET 4+ frameworks, but essentially this is expected.

While you think it is 4.0 it's in fact 4.2. Dll hell, welcome back!

answered on Stack Overflow Oct 26, 2018 by Access Denied • edited Oct 26, 2018 by Access Denied

User contributions licensed under CC BY-SA 3.0