1

I'm encountering an error in my Blazor WebAssembly application running on .NET 8. The error occurs when the application launches:

InvalidOperationException: Cannot provide a value for property 'Http' on type 'SGC.Client.Layout.NavMenu'. There is no registered service of type 'System.Net.Http.HttpClient'.

This is my Program.cs file on the client-side:

var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.Services.AddHttpClient();
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();

builder.Services.AddRadzenComponents();

builder.Services.AddScoped<DialogService>();
builder.Services.AddScoped<NotificationService>();
builder.Services.AddScoped<ContextMenuService>();
builder.Services.AddScoped<TooltipService>();

await builder.Build().RunAsync();

The error occurs when I inject HttpClient into any page. For example:

@page "/example"
@inject HttpClient Http

<h3>Example Page</h3>

How can I resolve this issue and correctly inject HttpClient into my Blazor WebAssembly application?

Ensured HttpClient is registered in the services in Program.cs.

3
  • 2
    What is happening here is the page is first rendered on the server using SSR so the code in your razor page is running on the server expecting to be able to inject a HttpClient. You can either configure the same injection on the server or disable pre-render. Disabling maybe tempting as it's easier, however SSR is more performant for the user. Commented Jul 12, 2024 at 3:52
  • Another workaround is to @inject IEnumerable<HttpClient> HttpClients - as this will not throw if there are none registered, but you do then have to add code to get the HttpClient from the IEnumerable and allow for nulls. Commented Jul 12, 2024 at 7:52
  • @BrianParker is right, see also the microsoft documentation learn.microsoft.com/en-us/aspnet/core/blazor/… Commented Jul 12, 2024 at 11:03

1 Answer 1

0

You want to use IHttpClientFactory when creating HttpClients, because there's some underlying code that manages the lifetime of sockets etc. You can read more of it here to decide the best way of implementing your required DI pattern:

https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net-core

Sign up to request clarification or add additional context in comments.

2 Comments

Don't you love markdowns without a comment... what I said is correct and I would suggest people read the correct documentation - HttpClient is a special object that you shouldn't be creating yourself as the whole IHttpClientFactory manages internal object / connections / sockets properly
Seems like a good reply to me, I gave you a vote! My only suggestion for improvement would be to add a code snippet showing the basic usage, and then link to the docs. However, even without that, a downvote seems harsh to me, but then that's SO for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.