I am trying to figure out how to define multiple http clients for my Blazor app.
I have an client defined for my Blazor API, now I need a couple more for 3rd party resources. I don't see how to "name" them so that I can request them at in my business logic.
My business classes are like this
class AccountsRepository{
public AccountsRepository(HttpClient client).... --//This one should be calling back to my controllers
}
class ThirdParty1Services{
public ThirParty1Services(HttpClient client)... --//This one should be calling to 3rd party #1
}
class ThirdParty2Services{
public ThirParty2Services(HttpClient client)... --//This one should be calling to 3rd party #2
}
in my Program.cs of the Blazor client, I have
builder.Services.AddHttpClient("VEMS.Server", client => {
client.BaseAddress = new System.Uri(builder.HostEnvironment.BaseAddress);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
builder.Services.AddHttpClient("NHTSA.API", client => {
client.BaseAddress = new System.Uri("https://vpic.nhtsa.dot.gov/api/");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
})
builder.Services.AddHttpClient("Legacy.API", client => {
client.BaseAddress = new System.Uri("https://www.mydomain.com/legacy/api/");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
})
I then register my services:
builder.Services.AddScoped<IAccountsRepository, AccountsService>();
builder.Services.AddScoped<INhtsa, NhtsaService>();
builder.Services.AddScoped<ILegacySupport, LegacyService>();
If you guys can point me in the right direction, I would very much appreciate it.