enet
enet

Reputation: 45626

WebClient not supported

Trying to use WebClient in Blazor project.

Getting following error:

In blazor.webassembly.js:1

WASM: System.Net.WebException: An exception occurred during a WebClient request.
System.PlatformNotSupportedException: Operation is not supported on this platform.

How to make an API/Network call in Blazor?

Upvotes: 6

Views: 7411

Answers (1)

enet
enet

Reputation: 45626

WebClient is not supported on Blazor. You should use HttpClient instead. In Blazor client side HttpClient is provided by the framework as a service; thus, you can inject it into your components or normal classes. In server-side Blazor this offer is missing, and you should create and configure the HttpClient yourself. You can also use IHttpClientFactory to provide HttpClient, which is preferable.

Note: WebClient cannot be used on the Browser (client-side Blazor) simply because it is relatively old technology, and there is no reason to adapt it to use on the browser. You probably believed that it is possible because HttpClient can run on the browser, but it may be a good idea to clarify here that when you use HttpClient on the browser, you don't actually use the actual HttpClient, which may use websockets, etc, but an adaptation form of HttpClient, which behind the scene use the JavaScript Fetch API.

Hope this helps...

Good news:

Moving to the mono.wasm bindings should enable support for WebSockets, HttpClient without addition code on our side, etc.

Source: https://github.com/aspnet/AspNetCore/issues/10489

Upvotes: 8

Related Questions