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>
MAK
  • 1,250
  • 21
  • 50
  • Is your app actually targeting .NET Framework? – Chris Pratt Oct 11 '18 at 16:34
  • netcoreapp2.1 – MAK Oct 11 '18 at 17:22
  • @MAK 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. – Ross Brasseaux Oct 30 '18 at 15:39
  • @Lopsided From the repository of the project, it seems it requires .Net 4.5 and is not compatible with .Net core at the moment. https://github.com/Microsoft/PowerBI-CSharp/blob/cc59b14986666afeaf2589fd4c31ea6ec4e58146/sdk/PowerBI.Api.Tests/PowerBI.Api.Tests.csproj – Dipen Shah Oct 30 '18 at 18:00
  • @Lopsided. I manually the 2 dll's in the project. Please see update on my question. – MAK Oct 31 '18 at 13:32
  • 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. – Ross Brasseaux Oct 31 '18 at 15:38
  • 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 disk – Society Guru Nov 07 '18 at 22:40

3 Answers3

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.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • 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. – MAK Oct 11 '18 at 18:07
  • 1
    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. – Chris Pratt Oct 11 '18 at 18:49
  • @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. – Ross Brasseaux Nov 07 '18 at 15:45
  • @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. – Chris Pratt Nov 07 '18 at 15:50
  • @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. – Ross Brasseaux Nov 07 '18 at 15:56
  • 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. – Chris Pratt Nov 07 '18 at 16:02
  • @ChrisPratt Well said, Chris. Thanks for the explanation. – Ross Brasseaux Nov 07 '18 at 17:26
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
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
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.

Fka
  • 6,044
  • 5
  • 42
  • 60