asp.netasp.net-core-2.0system.netsystem.net.httpwebrequest

FileNotFoundException: Could not load file or assembly 'System.Net.Http.WebRequest'


I've downloaded the latest .NET Framework and I'm working on .NET Core 2.0 Application on VS 2017 15.8.7. Here are the packages I've installed.

enter image description here

using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
      {

      }

I'm getting an error at this line, saying:

FileNotFoundException: Could not load file or assembly 'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Here is my .csproj

 <PackageReference Include="Microsoft.AspNetCore.App" />
 <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
 <PackageReference Include="Microsoft.PowerBI.Api" Version="2.0.14" />
 <PackageReference Include="Microsoft.PowerBI.Core" Version="1.1.11" />
 <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.6" />
 <PackageReference Include="System.Net.Http" Version="4.3.4" />

Why am I getting this error. Is there a reference I can add to make it work?

[UPDATE] I added the following lines in my csproj and am no longer getting this error.

<ItemGroup>
    <Reference Include="System.Net.Http">
      <HintPath>..\..\..\..\..\..\Windows\Microsoft.NET\Framework\v4.0.30319\System.Net.Http.dll</HintPath>
    </Reference>
    <Reference Include="System.Net.Http.WebRequest">
      <HintPath>..\..\..\..\..\..\Windows\Microsoft.NET\Framework\v4.0.30319\System.Net.Http.WebRequest.dll</HintPath>
    </Reference>
  </ItemGroup>

Solution

  • There's your problem. You're targeting .NET Core. The code you're using uses WebRequest under the hood, which doesn't exist in .NET Core. You'll need to target the full framework:

    <TargetFramework>net461</TargetFramework>
    

    Or whatever version you want to target. That of course means you can only run this app on a Windows server.