【发布时间】:2021-03-13 15:00:18
【问题描述】:
在 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();
}
}
好的,最终在我的所有服务中将 HttpClient 替换为 IHttpClientFactory
public UserService(IHttpClientFactory clientFactory)
{
_client = clientFactory.CreateClient("SamsonApi");
}
【问题讨论】:
-
为什么要注入http客户端?您始终可以通过服务构造函数中的名称注入工厂并创建客户端。
标签: c# dependency-injection blazor-client-side scrutor