Blazor Wasm Multiple HttpClients

Matthew Holton 331 Reputation points
2021-08-19T20:34:09.55+00:00

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.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,500 questions
0 comments No comments
{count} votes

Accepted answer
  1. Stefan Khane 76 Reputation points
    2021-08-19T21:33:19.907+00:00

    What you need to do is bind your service and client together like this:

    builder.Services.AddHttpClient<NHTSAClient>("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"));
                });
    

    And your client would look like:

    public class NHTSAClient
        {
            public NHTSAClient(HttpClient client)
            {
                Client = client;
            }
    
            public HttpClient Client { get; }
        }
    

    So now, from your service, you would just do this:

    private readonly NHTSAClient _client;
    
            public NHTSAService(NHTSAClient client)
            {
                _client = client;
            }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2021-08-19T21:37:44.873+00:00

    First be sure your 3rd party api support CORS or you will not be able to call them. On blazor WASM httpclient just uses JavaScript to make network calls, and is restricted by the browser rules.

    I would probably use my main api as a proxy to the 3rd party apis

    0 comments No comments