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

3

8

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>
Bopp answered 11/10, 2018 at 15:46 Comment(7)
Is your app actually targeting .NET Framework?Gizmo
<PropertyGroup> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup>Bopp
@Bopp I'm having this problem now, did you find a solution? Dotnet core is not the same as .NET Framework, and it is not clear for me right now whether the PowerBI API client is compatible with dotnet core--it builds, but I get this error at runtime. However, I ported my application over to .NET Framework 4.6.1 and I am still getting this error at runtime, so maybe it is something else.Polar
@Lopsided From the repository of the project, it seems it requires .Net 4.5 and is not compatible with .Net core at the moment. github.com/Microsoft/PowerBI-CSharp/blob/…Hebdomad
@Lopsided. I manually the 2 dll's in the project. Please see update on my question.Bopp
I take it back. Porting the app to .NET Framework did fix the issue. I had two VS instances open and for some reason the debugger on one was hosting the project from the other...which itself is a very strange issue too I think, but not relevant here.Polar
I thinks there just need to update the first parameter to be parse to the url must include a canonical address making reference to the diskFaria
G
5

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.

Gizmo answered 11/10, 2018 at 18:0 Comment(7)
If I change the Target Framework to net461 I get errors: Package Microsoft.AspNetCore.App 2.1.0 is not compatible with net461. My application is a .net core 2.0 app.Bopp
Then try bumping the version to something newer. I'm honestly not sure if any version of .NET Framework is compatible with ASP.NET Core 2.1. If not, you may have to downgrade to 2.0.Gizmo
@ChrisPratt I gave you the bounty because technically you are correct and this is still the best answer to the question and solution to the problem. HOWEVER, your comments regarding .NET Framework imply some misunderstandings regarding the relationship between .NET Framework, .NET Core, and .NET Standard. Note that .NET Core is a rewrite, and not some "core" portion of the .NET Framework. You may very well know this already (your account is certainly reputable enough to suggest that is the case), but the language you use here is slightly confusing and may mislead other developers.Polar
@Lopsided: I'm not sure I see where the language is confusing. WebRequest exists as essentially a Windows-only API, which means the full framework must be targeted to support the inclusion of the library which uses it. This is the precise reason you get a compiler warning when referencing a .NET Framework library in a project that targets .NET Core: there might be APIs used that are not supported by .NET Core and, more specifically, .NET Standard.Gizmo
@ChrisPratt Perhaps I am being pedantic, but the phrase "you'll need to target the full framework" implies to me that .NET Framework is an more robust version / implementation of .NET Core. It is the reason I discounted your answer in the first place and went about setting the bounty to bait a more reliable source.Polar
That phrase has been used by many since the introduction of .NET Core to refer to .NET Framework. Particularly during the 1.X days when it was more common to need to target .NET Framework in an ASP.NET Core app, because .NET Core was till missing many important features. Now that .NET Core has a much broader API footprint and in some cases exceeds .NET Framework, I guess I can see your point somewhat, but you get used to referring to things in a certain way.Gizmo
@ChrisPratt Well said, Chris. Thanks for the explanation.Polar
O
2

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
Ostracoderm answered 6/11, 2018 at 10:26 Comment(1)
Thanks for this. Ran into this issue and your first bullet, adding the nuget pkg to the main client project, fixed our issue.Toneytong
H
0

Sometimes it helps to install highest possible version of missing package on NuGet (System.Net.Http). It may happen, that ASP.NET uses different version than PowerBI and binding redirects may be required.

Horrible answered 6/11, 2018 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.