Aspnetcore: Blazor HttpClientHandler.SendAsync is wiped

Created on 5 Apr 2019  路  9Comments  路  Source: dotnet/aspnetcore

Describe the bug

Blazor HttpClientHandler.SendAsync is wiped. Similar issue here https://github.com/aspnet/Blazor/issues/1484 that mentions a correction in 0.7.0 and working with the mono team to add support.
I am using Blazor 0.9.0-preview3-19154-02.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core '3.0.100-preview3-010431'
  2. Invoke the SendAsync method on the HttpClientHandler

Expected behavior

Expect the method to invoke a web request

Additional context

Unexpected error in HttpContent POST with result: Cannot invoke method because it was wiped. See stack trace for details.
System.NotImplementedException: Cannot invoke method because it was wiped. See stack trace for details.
components.webassembly.js:1 WASM:
at System.Net.Http.HttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)

area-blazor investigate

Most helpful comment

BTW as a workaround, I think you can do something like:

services
    .AddHttpClient<IMyClient, MyClient>()
    .ConfigurePrimaryHttpMessageHandler<WebAssemblyHttpMessageHandler>();

All 9 comments

Thanks for contacting us, @joelhays.
Can you please share a repro project so we can investigate this better?
/cc @SteveSandersonMS thoughts?

@mkArtakMSFT, I'm working on creating a minimal example that I can share.

In the meantime, here's some additional information I found:

If I create a brand new HttpClient before every request, it works just fine. However, I have nested object structure that's creating the client and holding a reference to it for the entire lifecycle. When I try to submit a request with that reuseable HttpClient, that's when the issue occurs.

If it helps, I have a queue of requests and on an interval, I process the queue and submit all requests via the shared client through a series of async tasks.

This error only appears to occur when i @inject the service that's using the HttpClient. If i instantiate a local instance in the component on init, it all works fine as well. So seems like some sort of bug with injecting.

Thanks for reporting it. Since this doesn't happen in default cases, we'll need some minimal repro code to understand what's different in your scenario.

Hi. We're closing this issue as we have heard no response from you for some time now. If you have more details and are encountering this issue please add a new reply and re-open the issue.

Hi @mkArtakMSFT , the issue is indeed there. Here's how it can be reproduced:

  1. File>New>Blazor Project
  2. Create a typed client:
    public class GitHub
    {
        private readonly HttpClient _client;
        public GitHub(HttpClient client)
        {
            client.BaseAddress = new Uri("https://api.github.com/");
            client.DefaultRequestHeaders.Add("Accept",
                "application/vnd.github.v3+json");
            client.DefaultRequestHeaders.Add("User-Agent",
                "HttpClientFactory-Sample");
            this._client = client;
        }

        public async Task<string> GetAspNetDocsIssues()
        {
            var response = await _client.GetAsync(
                $"{_client.BaseAddress}repos/aspnet/AspNetCore.Docs/issues?state=open&sort=created&direction=desc");
            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsStringAsync();
            return result;
        }
    }
  1. Configure DI in Startup.cs as:
services.AddHttpClient<GitHub>();
  1. Now in FetchData.rajor - Inject the typed client
@inject GitHub Git
  1. Finally, invoking the API:
    protected async override Task OnAfterRenderAsync()
    {
        // THIS WILL FAIL !!!
        await Git.GetAspNetDocsIssues();
        await base.OnAfterRenderAsync();
    }

image

Maybe reopen the issue?

This fails because AddHttpClient doesn't supply HttpClient instances that use the correct message handler (WebAssemblyHttpMessageHandler).

This will only be fixed once we implement #10489. So rather than reopening this issue, we can regard it as covered by #10489.

BTW as a workaround, I think you can do something like:

services
    .AddHttpClient<IMyClient, MyClient>()
    .ConfigurePrimaryHttpMessageHandler<WebAssemblyHttpMessageHandler>();

Thanks @SteveSandersonMS, workaround works, Cheers!

I came here while trying to use autorest generated SDK. Turns out HttpClient can be injected to autorest generated SDK from blazor and it just works fine. Here is the approach: https://stackoverflow.com/a/49953565/147613. Leaving it hear for anyone who faces same issue.

Was this page helpful?
0 / 5 - 0 ratings