Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Base address of HttpClient is getting null for the first time in OnInitializedAsync method in Blazor

$
0
0

In my Blazor web app(auto render mode), I have added the http client as service in server project like bellow:

builder.Services.AddHttpClient();

I also added http client in the web assembly (client) project like bellow:

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

Now the problem is, when I inject HttpClient in my home page component(in client project), and try to use that service in OnInitializedAsync method, it throws an exception first time saying no base address is added, but in the OnAfterRenderAsync method the http client works fine with the correct base address.

What should I do to use the HttpClient in OnInitializedAsync method?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>