从注入 DI 的 IHttpClientFactory 获取名称 HttpClient

Mar*_*sen 1 c# dependency-injection scrutor blazor-webassembly

在 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是必须更改的代码。它扫描我的所有服务并注入 HttpClient。由于我现在有两个,所以我会随机注入一个客户端。如何将指定客户端注入到我的所有服务中?我可以将图形 API 服务作为特殊情况来处理。

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

调用我的 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

public UserService(IHttpClientFactory clientFactory)
{
   _client = clientFactory.CreateClient("SamsonApi");
}
Run Code Online (Sandbox Code Playgroud)

Xer*_*lio 5

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

在这种情况下,您可以让您的类依赖,然后使用命名客户端IHttpClientFactory设置配置:

// 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

...或者使用类型化客户端,您可以为依赖于它的每个类指定实例:

// 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();
    }
}
Run Code Online (Sandbox Code Playgroud)