Базовый адрес HttpClient впервые становится нулевым в методе OnInitializedAsync в BlazorC#

Место общения программистов C#
Гость
Базовый адрес HttpClient впервые становится нулевым в методе OnInitializedAsync в Blazor

Сообщение Гость »


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?


Источник: https://stackoverflow.com/questions/781 ... tializedas

Вернуться в «C#»