将 httpHandler 附加到 httpclientFactory webapi aspnetcore 2.1
Attaching httpHandler to httpclientFactory webapi aspnetcore 2.1
我正在尝试使用 "ConfigurePrimaryHttpMessageHandler"
将处理程序附加到 httpclientfactory
但是当我查看 HttpClient 内部时
如果处理程序在那里我找不到它
我是否正确附加了处理程序?
任何建议
services.AddHttpClient<IGitHubClient,GitHubClient>(client =>
{
client.BaseAddress = new Uri(myUri);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
})
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
});
public interface IGitHubClient
{
Task<string> GetData();
}
public class GitHubClient : IGitHubClient
{
private readonly HttpClient _client;
public GitHubClient(HttpClient httpClient)
{
_client = httpClient;
}
public async Task<string> GetData()
{
return await _client.GetStringAsync("/");
}
}
public class ValuesController : Controller
{
private readonly IGitHubClient _gitHubClient;;
public ValuesController(IGitHubClient gitHubClient)
{
_gitHubClient = gitHubClient;
}
[HttpGet]
public async Task<ActionResult> Get()
{
//my _gitHubClient has no Handler attached!!!
string result = await _gitHubClient.GetData();
return Ok(result);
}
}
您显示的代码是推荐的方法。
关于_gitHubClient
的评论
//my _gitHubClient has no Handler attached!!!
好像是个误会。
_gitHubClient
是您的抽象,它在其 GitHubClient
实现中包装了一个 HttpClient
实例。
public class GitHubClient : IGitHubClient {
private readonly HttpClient _client; //<< Handler will be attached to this instance
public GitHubClient(HttpClient httpClient) {
_client = httpClient;
}
public async Task<string> GetData() {
return await _client.GetStringAsync("/");
}
}
它是具有附加处理程序的包装实例。
根据当前配置,每当框架必须创建 IGitHubClient
派生 GitHubClient
的实例进行注入时,工厂将使用提供的设置创建 HttpClient
启动。其中还包括添加 ConfigurePrimaryHttpMessageHandler
提供的 HttpClientHandler
我正在尝试使用 "ConfigurePrimaryHttpMessageHandler"
将处理程序附加到 httpclientfactory但是当我查看 HttpClient 内部时 如果处理程序在那里我找不到它
我是否正确附加了处理程序?
任何建议
services.AddHttpClient<IGitHubClient,GitHubClient>(client =>
{
client.BaseAddress = new Uri(myUri);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
})
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
});
public interface IGitHubClient
{
Task<string> GetData();
}
public class GitHubClient : IGitHubClient
{
private readonly HttpClient _client;
public GitHubClient(HttpClient httpClient)
{
_client = httpClient;
}
public async Task<string> GetData()
{
return await _client.GetStringAsync("/");
}
}
public class ValuesController : Controller
{
private readonly IGitHubClient _gitHubClient;;
public ValuesController(IGitHubClient gitHubClient)
{
_gitHubClient = gitHubClient;
}
[HttpGet]
public async Task<ActionResult> Get()
{
//my _gitHubClient has no Handler attached!!!
string result = await _gitHubClient.GetData();
return Ok(result);
}
}
您显示的代码是推荐的方法。
关于_gitHubClient
的评论
//my _gitHubClient has no Handler attached!!!
好像是个误会。
_gitHubClient
是您的抽象,它在其 GitHubClient
实现中包装了一个 HttpClient
实例。
public class GitHubClient : IGitHubClient {
private readonly HttpClient _client; //<< Handler will be attached to this instance
public GitHubClient(HttpClient httpClient) {
_client = httpClient;
}
public async Task<string> GetData() {
return await _client.GetStringAsync("/");
}
}
它是具有附加处理程序的包装实例。
根据当前配置,每当框架必须创建 IGitHubClient
派生 GitHubClient
的实例进行注入时,工厂将使用提供的设置创建 HttpClient
启动。其中还包括添加 ConfigurePrimaryHttpMessageHandler
HttpClientHandler