Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor does not support System.Net.Http.HttpClientHandler #42405

Closed
linvi opened this issue Sep 17, 2020 · 7 comments
Closed

Blazor does not support System.Net.Http.HttpClientHandler #42405

linvi opened this issue Sep 17, 2020 · 7 comments
Assignees
Labels
arch-wasm WebAssembly architecture area-System.Net
Milestone

Comments

@linvi
Copy link

linvi commented Sep 17, 2020

Describe the bug

Trying to use an HttpClientHandler in blazor results in:

WASM: System.PlatformNotSupportedException: System.Net.Http.HttpClientHandler is not supported on the current platform.
WASM:   at System.Net.Http.HttpClientHandler.set_UseCookies (System.Boolean value) <0x34686c8 + 0x0000c>

or

 System.NotImplementedException: Cannot invoke method because it was wiped. See stack trace for details.

To Reproduce

  • Create a Blazor WebAssembly 3.0 project.
  • Modify a page and put the following code:
@page "/counter"

<h1>Example</h1>
<p>Status: @status</p>

<button class="btn btn-primary" @onclick="Try">Click...</button>

@code {
    public class ClientHandler : HttpClientHandler
    {
    }

    string status = "Not yet loaded...";

    async Task Try()
    {
        status = "loading...";

        var client = new HttpClient(new ClientHandler());
        var response = await client.GetAsync("https://www.google.com");

        status = response.IsSuccessStatusCode ? "success" : "failure";
    }
}
  • Click on the button

You can also try with the Tweetinvi library that intensively use custom HttpClientHandlers

dotnet add package TweetinviApi -v 5.0.0-alpha-8

@page "/counter"
@using Tweetinvi.Models
@using Tweetinvi

<h1>Counter</h1>

<p>Username: @username</p>

<button class="btn btn-primary" @onclick="LoadUser">Load user</button>

@code {
    string username = "Not yet loaded...";

    async Task LoadUser()
    {
        username = "loading..."
    
        var creds = new TwitterCredentials("CONSUMER_KEY",
            "CONSUMER_SECRET",
            "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");

        ITwitterClient client = new TwitterClient(creds);
        var user = await client.Users.GetAuthenticatedUserAsync();
        username = user.Name;
    }
}

Exceptions (if any)

WASM: System.PlatformNotSupportedException: System.Net.Http.HttpClientHandler is not supported on the current platform.
WASM:   at System.Net.Http.HttpClientHandler.set_UseCookies (System.Boolean value) <0x34686c8 + 0x0000c>

Further technical details

  • ASP.NET Core version

Microsoft.AspNetCore.Blazor : Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60

  • Include the output of dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.201
 Commit:    b1768b4ae7

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.15
 OS Platform: Darwin
 RID:         osx.10.15-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.1.201/

Host (useful for support):
  Version: 3.1.3
  Commit:  4a9f85e9f8

.NET Core SDKs installed:
  2.1.4 [/usr/local/share/dotnet/sdk]
  2.1.503 [/usr/local/share/dotnet/sdk]
  2.1.504 [/usr/local/share/dotnet/sdk]
  2.1.505 [/usr/local/share/dotnet/sdk]
  2.2.104 [/usr/local/share/dotnet/sdk]
  2.2.401 [/usr/local/share/dotnet/sdk]
  3.0.103 [/usr/local/share/dotnet/sdk]
  3.1.101 [/usr/local/share/dotnet/sdk]
  3.1.200 [/usr/local/share/dotnet/sdk]
  3.1.201 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.16 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
  • Rider MacOS
@campersau
Copy link
Contributor

Yes, not all features of HttpClientHandler are supported inside the browser sandbox. See

public bool UseCookies
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public CookieContainer CookieContainer
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public DecompressionMethods AutomaticDecompression
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public bool UseProxy
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public IWebProxy? Proxy
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public ICredentials? DefaultProxyCredentials
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public bool PreAuthenticate
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public ICredentials? Credentials
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public bool AllowAutoRedirect
{
get => _allowAutoRedirect;
set
{
_allowAutoRedirect = value;
_isAllowAutoRedirectTouched = true;
}
}
public int MaxAutomaticRedirections
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public int MaxConnectionsPerServer
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public int MaxResponseHeadersLength
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public SslClientAuthenticationOptions SslOptions
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

I guess tweetinvi would need to be updated to account for that here https://github.com/linvi/tweetinvi/blob/0381167535872ba94b89237213b6929536449476/src/Tweetinvi.WebLogic/TwitterClientHandler.cs#L22-L23

@pranavkm pranavkm transferred this issue from dotnet/aspnetcore Sep 17, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Net untriaged New issue has not been triaged by the area owner labels Sep 17, 2020
@ghost
Copy link

ghost commented Sep 17, 2020

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@pranavkm
Copy link
Contributor

Thanks @campersau for responding to this. For rc2, we've added an analyzer that should tell you at build time if these APIs are going to be unavailable.

/cc @kjpou1 in case you'd like to add more content.

@lewing lewing added arch-wasm WebAssembly architecture and removed untriaged New issue has not been triaged by the area owner labels Sep 17, 2020
@kjpou1
Copy link
Contributor

kjpou1 commented Sep 18, 2020

Not sure what else can be added. The HttpClient uses the browsers underlying fetch implementation. Fetch does not support Cookies thus the Properties referencing cookies are marked as not supported.

Will close for now. If someone sees this in error please reopen.

@kjpou1 kjpou1 closed this as completed Sep 18, 2020
@wfurt
Copy link
Member

wfurt commented Sep 18, 2020

is it not possible to set cookie header @kjpou1 ? From the link it seems like headers can be passed in...?

@campersau
Copy link
Contributor

You can set some custom headers but not for cookies, they are handled entirely by the browser. You can only tell the browser to include them by setting the credentials option.
See JakeChampion/fetch#349

Also sending custom headers only works partially when doing cross domain requests.
See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch and https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Browser specific settings like including credentials (aka cookies) in cross domain requests can be set via these methods:
https://github.com/dotnet/aspnetcore/blob/f52077f599f0d3b4545d8f87a0c3d15217b33069/src/Components/WebAssembly/WebAssembly/src/Http/WebAssemblyHttpRequestMessageExtensions.cs

@kjpou1
Copy link
Contributor

kjpou1 commented Sep 21, 2020

Thanks for the follow up @campersau

@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
@karelz karelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-System.Net
Projects
None yet
Development

No branches or pull requests

8 participants