32

Is there any way to use a WebClient in a .NET Core application? If I build the application I get the following error:

Severity    Code    Description Project File    Line
Error   CS0246  The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)

I think WebClient is not a part of .NET Core, but is there any alternative?

Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
TimBoss
  • 545
  • 1
  • 5
  • 14
  • 6
    I don't see `WebClient` any where in the corefx repo, `HttpClient` would probably be the alternative. – Mike Zboray May 14 '15 at 00:37
  • 2
    I added "Microsoft.Net.Http.Client": "1.0.0-beta3-10053" in project.json and i can instantiate a new HttpClient. And it works even with .NetCore. Maybe it is the single purpose to handle this. – TimBoss May 14 '15 at 01:31
  • Possible duplicate of [System.Net.Webclient not working 'WebClient' could not be found](http://stackoverflow.com/questions/19164900/system-net-webclient-not-working-webclient-could-not-be-found) – Pranav Singh Jul 13 '16 at 07:09

2 Answers2

35

As of .Net Standard 2.0, WebClient is now available to any implementations of the standard, including .Net Core. However, the Stack Overflow question "Need help deciding between HttpClient and WebClient" has some fairly good answers as to why you should be using the HttpClient instead.

One of the drawbacks mentioned is that there is no built-in progress reporting in the HttpClient. However, because it is using streams, it is possible to write your own. The answers to "How to implement progress reporting for Portable HttpClient" provides an example for reporting the progress of the response stream.

If you're targeting prior versions of the standard, you'll need to use HttpClient as WebClient is not available prior to .Net Standard 2.0.

Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
4

WebClient is now available in .Net Standard 2.0 https://learn.microsoft.com/en-gb/dotnet/api/system.net.webclient?view=netstandard-2.0

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83