Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 849
MAK Avatar asked Oct 11 '18 15:10

MAK


2 Answers

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.

like image 132
Chris Pratt Avatar answered Oct 07 '22 00:10

Chris Pratt


I know 2 situations where you can get this error:

  • the nuget package is not installed in the "client project" of the solution (it is NOT enough to add dependency to a common/factorized project of the solution; sometimes you need to add the dependency to the project using it, itself)
  • your defined Framework version is not compatible either among all project of your solution, or with the already installed nuget package; you may consider making a big upgrade of all your nuget packages, and check the Framework version defined on each of your projects
like image 25
Bsquare ℬℬ Avatar answered Oct 07 '22 02:10

Bsquare ℬℬ