HttpClient showing not registered Blazor WebAssembly

Prathamesh Shende 441 Reputation points
2025-04-23T16:18:32.4033333+00:00

How to consume WebApi in blazor webassembly in .net 8 or 9.

I added scope Http Client but am still getting the error not registered Http Client.

  1. My project is the render InteractiveWebAssembly Project, which has two project Client and Main.
  2. main project has a component folder, and the client project has a pages folder.

I really don't understand the project structure of either.
If possible to you please share me the link where I learn project structure.

For HttpClient in BlazorWebassembly I do as this link -
https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-9.0
but still getting error.

Blazor Training
Blazor Training
Blazor: A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.Training: Instruction to develop new skills.
32 questions
{count} votes

Accepted answer
  1. Pradeep M 8,660 Reputation points Microsoft External Staff Moderator
    2025-04-25T03:52:13.5066667+00:00

    Hi Prathamesh Shende,

    Thank you for reaching out to Microsoft Q & A forum. 

    The error message InvalidOperationException: Cannot provide a value for property 'http'...suggests that the HttpClient service has not been correctly registered in the application's service container. When working with Blazor applications that use the Interactive WebAssembly render mode in .NET 8 or 9, it is essential to register services such as HttpClient in the Main project (in your case, BlazorApp2), rather than only in the Client project. This ensures that the service is available both during server-side prerendering and when the WebAssembly app runs on the client. 

    To resolve the issue, update the Program.cs file in the Main project as follows: 

    builder.Services.AddHttpClient("BlazorApp2.ServerAPI", client =>
    {
        client.BaseAddress = new Uri("https://localhost:7056");
    });
    builder.Services.AddScoped(sp =>
        sp.GetRequiredService<IHttpClientFactory>().CreateClient("BlazorApp2.ServerAPI"));
    builder.Services.AddScoped<IWeatherForecastService, WeatherForecastService>();
    
    

    Make sure that your components inject IWeatherForecastService rather than HttpClient directly, unless HttpClient has been explicitly registered as shown above. 

    For additional guidance, please refer to the links that AgaveJoe shared earlier, which cover Blazor render modes, project structure, and sample implementations.  

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.  

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.