gpt4 book ai didi

c# - 配置 HttpClientFactory 以使用当前请求上下文中的数据

转载 作者:行者123 更新时间:2023-12-03 05:01:46 27 4
gpt4 key购买 nike

随着新的HttpClientFactory在 ASP.NET Core 2.1 中,使用基本 url、默认 header 等配置自定义 HTTP 客户端非常容易。

但是,我还没有找到一种集中配置的方法,可以让我从当前请求上下文中注入(inject) header 。例如,考虑使用 Authorization 调用的服务。 header ,我也希望将其传递给所有底层服务。能够在 .AddHttpClient() 中配置它会很棒。调用servicesStartup类,但我不知道如何从那里获取请求上下文。

有任何想法吗?

最佳答案

处理这个答案让我得到了多个答案。我认为第一种方法是您正在寻找的方法,第二种方法是一个不错的选择。

为了配置多个客户端,您可以使用 named clients .这些客户端被注册为 transient 。使用 DI 获取有权访问请求上下文的服务。

为此,我们需要 IHttpContextAccessor。在这种情况下,您不必自己注册,因为 Identity 已经为您完成了注册。

否则在启动时添加以下行:

services.AddHttpContextAccessor();

接下来我们可以配置命名客户端“github”:
services.AddHttpClient("github", c =>
{
// access the DI container
var serviceProvider = services.BuildServiceProvider();
// Find the HttpContextAccessor service
var httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>();
// Get the bearer token from the request context (header)
var bearerToken = httpContextAccessor.HttpContext.Request
.Headers["Authorization"]
.FirstOrDefault(h => h.StartsWith("bearer ", StringComparison.InvariantCultureIgnoreCase));

// Add authorization if found
if (bearerToken != null)
c.DefaultRequestHeaders.Add("Authorization", bearerToken);

// Other settings
c.BaseAddress = new Uri("https://api.github.com/");
c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); // Github API versioning
c.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample"); // Github requires a user-agent
});

像这样调用客户端:
public class MyController : ControllerBase
{
private readonly IHttpClientFactory _clientFactory;

public MyController(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}

public async Task<ActionResult> StartCall()
{
var client = _clientFactory.CreateClient("github");
var response = await client.GetAsync("/repos/aspnet/docs/issues");
}
}

另一种选择是使用 Typed 客户端。这是一个简短的例子。有关完整示例,请查看 link .

注册 IHttpContextAccessor:
services.AddHttpContextAccessor();

创建一个类型化的客户端。我添加了两个选项来添加设置。一个通过请求上下文,一个通过单例类:
public class GitHubService
{
public HttpClient Client { get; }

public GitHubService(HttpClient client, HttpClientSettings httpClientSettings, IHttpContextAccessor httpContextAccessor)
{
var bearerToken = httpContextAccessor.HttpContext.Request
.Headers["Authorization"]
.FirstOrDefault(h => h.StartsWith("bearer ", StringComparison.InvariantCultureIgnoreCase));

// Add authorization if found
if (bearerToken != null)
client.DefaultRequestHeaders.Add("Authorization", bearerToken);

// Or the value from httpClientSettings:
client.DefaultRequestHeaders.Add("Authorization", httpClientSettings.BearerToken);

client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); // GitHub API versioning
client.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample"); // GitHub requires a user-agent

Client = client;
}
}

注册客户端:
// The typed client is registered as transient with DI.
services.AddHttpClient<GitHubService>();

请注意,下面的代码只是一个示例。由于 token 无法在客户端中持久化,因此您可以使用共享的 HttpClientSettings 代替:
services.AddSingleton<HttpClientSettings>();

HttpClientSettings 是:
public class HttpClientSettings
{
public string BearerToken { get; set; }
}

您可以像这样使用客户端:
public class MyController : ControllerBase
{
private readonly GitHubService _gitHubService;

public MyController(GitHubService gitHubService)
{
_gitHubService = gitHubService;
}

public async Task<ActionResult> StartCall()
{
var response = await _gitHubService.Client.GetAsync("/repos/aspnet/docs/issues");

}
}

关于c# - 配置 HttpClientFactory 以使用当前请求上下文中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com