I have create
Blazore WASM App and with Identity Server 4 So
IHttpClientFactory and add default httpClient scope
but its showin Null HttpClient.BaseAddress after I fetching into services.cs file
this is files
Program.cs
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Logging.SetMinimumLevel(LogLevel.Debug);
//builder.Logging.AddProvider(new CustomLoggingProvider(LogLevel.Error));
builder.RootComponents.Add<App>("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// We register a named HttpClient here for the API
builder.Services.AddHttpClient("api")
.AddHttpMessageHandler(sp =>
{
var handler = sp.GetService<AuthorizationMessageHandler>()
.ConfigureHandler(
authorizedUrls: new[] { new Uri(builder.HostEnvironment.BaseAddress).ToString() },
scopes: new[] { "api1", "email" });
return handler;
});
// we use the api client as default HttpClient
builder.Services.AddScoped(sp => sp.GetService<IHttpClientFactory>().CreateClient("api"));
builder.Services
.AddOidcAuthentication(options =>
{
builder.Configuration.Bind("oidc", options.ProviderOptions);
options.UserOptions.RoleClaim = "role";
}).AddAccountClaimsPrincipalFactory<ArrayClaimsPrincipalFactory<RemoteUserAccount>>();
builder.Services.AddScoped<SetupsService>();
await builder.Build().RunAsync();
}
}
public class SetupsService
{
HttpClient _httpClient;
IHttpClientFactory _httpClientFactory;
public SetupsService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
_httpClient = _httpClientFactory.CreateClient("api");
}
public async Task<List<Setup>> GetSetupsAsync()
{
return await _httpClient.GetFromJsonAsync<List<Setup>>($"{ _httpClient.BaseAddress } api/Setups");
}
``}
here its show null and not fetching baseaddress