我正在尝试使用“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);
}
}