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

Unable to register HttpClient service #31267

Closed
BrettSandham opened this issue Dec 19, 2023 · 3 comments · Fixed by #31382
Closed

Unable to register HttpClient service #31267

BrettSandham opened this issue Dec 19, 2023 · 3 comments · Fixed by #31382
Assignees
Labels
Blazor doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@BrettSandham
Copy link

Description

Hi

I'm sure this is an ID10T user issue!

I am trying to follow https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-8.0&pivots=webassemblyn and cannot register an HttpClient Service. Minimum steps :

  • Create a new Project
  • Select "Blazor Web App" template
  • Choose Interactive render mode : WebAssembly
  • Add to Client.Program.cs
    builder.Services.AddScoped(sp =>
    new HttpClient
    {
    BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
    });
  • Add to Counter.razor
    @using System.Net.Http
    @Inject HttpClient Http
  • run project and navigate to Counter page

Error
An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot provide a value for property 'Http' on type 'HttpClientIssue.Client.Pages.Counter'. There is no registered service of type 'System.Net.Http.HttpClient'.

[Enter feedback here]

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/call-web-api.md

Document ID

c7e59a08-1c60-32c2-75fd-33cb77ff7a5d

Article author

@guardrex

Copy link
Contributor

🍂🎁 Happy Holidays! ❄️⛄

A green dinosaur 🦖 will be along shortly to assist. Stand-by ........

@guardrex
Copy link
Collaborator

guardrex commented Dec 19, 2023

Hello @BrettSandham ... That's due to prerendering. The service won't be available on the server, so the error is thrown.

It's covered in a general way here 👉 https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-8.0#client-side-services-fail-to-resolve-during-prerendering

For this specific case, there are two approaches to address it ...

Whichever direction you go, leave this issue open. I've been improving the guidance on these service scenarios, albeit slowly 🐌 due to all of the high priority issues that I'm still addressing. I'd like to use this issue to make further improvements to our coverage on this.

When I work on this, I'll take a couple of approaches to improve coverage ...

  • Add a section to this topic (WASM pivot) to call it out, name the two options that I just outlined, and cross-link to the Render Modes article for the general coverage.
  • Also, I don't think that I currently have a section in the Dependency Injection article on this subject. I'll work on covering this subject in both the Render Modes article, where it's currently covered, and in the DI article.

This issue will close automatically later when the PR merges, and I should be able to reach this in early '24. We're all just about to bug out of here for the ⛄🏂❄️ holiday break, so I won't be able to reach this until then.

Thanks for the issue and Happy New Year! 🎉🍾💃🕺🎆

@guardrex guardrex moved this from Triage to P0/P1 - High Priority in Blazor.Docs Dec 19, 2023
@BrettSandham
Copy link
Author

Thank you! Was not expecting such a prompt response this time of the year. All the best to you and the whole team.

@github-project-automation github-project-automation bot moved this from P0/P1 - High Priority to Done in Blazor.Docs Dec 20, 2023
@guardrex guardrex reopened this Dec 20, 2023
@github-project-automation github-project-automation bot moved this from Done to Triage in Blazor.Docs Dec 20, 2023
@guardrex guardrex moved this from Triage to 8.0 in Blazor.Docs Dec 20, 2023
@guardrex guardrex moved this from 8.0 to In progress in Blazor.Docs Jan 8, 2024
@github-project-automation github-project-automation bot moved this from In progress to Done in Blazor.Docs Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blazor doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants