从注入了 DI 的 IHttpClientFactory 获取名称 HttpClient

Get name HttpClient from IHttpClientFactory injected with DI

在 Blazor 中,我设置了两个 HttpClient。一份用于我的 API,一份用于 MS Graph API。 Graph API 是新的,迫使我找到一种方法将命名的 httpclient 注入到我的服务中。

这是 Main 中的所有代码

  public class Program
{
    public static async Task Main(string[] args)
    {
        var b = WebAssemblyHostBuilder.CreateDefault(args);
        b.RootComponents.Add<App>("app");

        var samsonApiUrl = new Uri(b.HostEnvironment.BaseAddress + "api/");

        b.Services.AddHttpClient("SamsonApi",client => 
        {
           client.BaseAddress = samsonApiUrl;
           // add jwt token to header
           // add user agent to header
        }).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
        
        b.Services.AddTransient<GraphCustomAuthorizationMessageHandler>();
        b.Services.AddHttpClient<GraphHttpClientService>("GraphAPI",
                client => client.BaseAddress = new Uri("https://graph.microsoft.com/"))
            .AddHttpMessageHandler<GraphCustomAuthorizationMessageHandler>();
        
        b.Services.AddScoped(provider => provider.GetService<IHttpClientFactory>().CreateClient("SamsonApi"));
        b.Services.AddScoped(provider => provider.GetService<IHttpClientFactory>().CreateClient("GraphAPI"));
        
        b.Services.AddMsalAuthentication<RemoteAuthenticationState, CustomUserAccount>(options =>
        {
            b.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
            options.ProviderOptions.DefaultAccessTokenScopes.Add("1c8d4e31-97dd-4a54-8c2b-0d81e4356bf9/API.Access");
            options.UserOptions.RoleClaim = "role";
        }).AddAccountClaimsPrincipalFactory<RemoteAuthenticationState, CustomUserAccount, CustomUserFactory>();
        
        
        // add Radzen services 
        b.Services.AddScoped<DialogService>();
        b.Services.AddScoped<NotificationService>();
        b.Services.AddScoped<TooltipService>();
        // add samson component services
        b.Services.AddSingleton<FormTitleState>();
        // Add Http Services
        b.Services.Scan(scan =>
            {
                scan.FromAssemblyOf<ICustomerService>()
                    .AddClasses(classes => classes.Where(type => type.Name.EndsWith("Service")))
                    .AsMatchingInterface()
                    .WithScopedLifetime();
            });
        await b.Build().RunAsync();
    }
}

这是必须更改的代码。 它扫描我的所有服务并注入一个 HttpClient。 因为我现在有两个,所以我得到了一个随机客户端注入。 我怎样才能将指定的客户端注入到我的所有服务中?我可以将图表 API 服务作为特例处理。

 b.Services.Scan(scan =>
            {
                scan.FromAssemblyOf<ICustomerService>()
                    .AddClasses(classes => classes.Where(type => type.Name.EndsWith("Service")))
                    .AsMatchingInterface()
                    .WithScopedLifetime();
            });

调用我的 API

的服务示例
 public class ActiveAgreementService : IActiveAgreementService
{
    private readonly HttpClient _client;
    
    public ActiveAgreementService(HttpClient client)
    {
        _client = client;
    }

    public async Task<List<ActiveAgreementDto>> GetActiveAgreements()
    {
        var lst = await _client.GetFromJsonAsync<ActiveAgreementDto[]>("ActiveAgreement");
        return lst.ToList();
    }
}

好吧,在我所有的服务中用 IHttpClientFactory 替换了 HttpClient

public UserService(IHttpClientFactory clientFactory)
{
   _client = clientFactory.CreateClient("SamsonApi");
}

我假设您使用的是 ASP.NET Core,尽管不清楚您使用的是哪个依赖注入框架。

在这种情况下,您可以让 classes 依赖于 IHttpClientFactory,然后使用 named clients:

设置配置
// Named client like you're currently doing
b.Services.AddHttpClient("SamsonApi", client => 
{
    client.BaseAddress = samsonApiUrl;
    // add jwt token to header
    // add user agent to header
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

//...

b.Services.AddHttpClient("GraphAPI", client =>
    client.BaseAddress = new Uri("https://graph.microsoft.com/"))
    .AddHttpMessageHandler<GraphCustomAuthorizationMessageHandler>();

// And in your dependent class
public class ActiveAgreementService : IActiveAgreementService
{
    private readonly HttpClient _client;
    
    public ActiveAgreementService(IHttpClientFactory clientFac)
    {
        // Whichever one you need:
        _client = clientFac.CreateClient("SamsonApi");
        _client = clientFac.CreateClient("GraphAPI");
    }

    public async Task<List<ActiveAgreementDto>> GetActiveAgreements()
    {
        var lst = await _client.GetFromJsonAsync<ActiveAgreementDto[]>("ActiveAgreement");
        return lst.ToList();
    }
}

... 或使用 typed clients 为依赖于它的每个 class 指定实例:

// This HttpClient is only injected into ActiveAgreementService
b.Services.AddHttpClient<ActiveAgreementService>(client => 
{
    client.BaseAddress = samsonApiUrl;
    // add jwt token to header
    // add user agent to header
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

//...

// This HttpClient is only injected into GraphHttpClientService
b.Services.AddHttpClient<GraphHttpClientService>(client =>
    client.BaseAddress = new Uri("https://graph.microsoft.com/"))
    .AddHttpMessageHandler<GraphCustomAuthorizationMessageHandler>();

// And in your dependent class
public class ActiveAgreementService : IActiveAgreementService
{
    private readonly HttpClient _client;
    
    public ActiveAgreementService(HttpClient client)
    {
        _client = client;
    }

    public async Task<List<ActiveAgreementDto>> GetActiveAgreements()
    {
        var lst = await _client.GetFromJsonAsync<ActiveAgreementDto[]>("ActiveAgreement");
        return lst.ToList();
    }
}