dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.56k stars 25.31k forks source link

Unable to register HttpClient service #31267

Closed BrettSandham closed 8 months ago

BrettSandham commented 9 months ago

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 :

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

github-actions[bot] commented 9 months ago

πŸ‚πŸŽ Happy Holidays! ❄️⛄

A green dinosaur πŸ¦– will be along shortly to assist. Stand-by ........

guardrex commented 9 months ago

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 :point_right: 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 ...

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! πŸŽ‰πŸΎπŸ’ƒπŸ•ΊπŸŽ†

BrettSandham commented 9 months ago

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