将httpHandler附加到httpclientFactory webapi aspnetcore 2.1 [英] Attaching httpHandler to httpclientFactory webapi aspnetcore 2.1

查看:126
本文介绍了将httpHandler附加到httpclientFactory webapi aspnetcore 2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用"ConfigurePrimaryHttpMessageHandler"将处理程序附加到httpclientfactory

I am trying to attach an handler to httpclientfactory using "ConfigurePrimaryHttpMessageHandler"

但是当我在HttpClient内部查看时 如果处理程序在那里,我找不到它

but when I look inside the HttpClient to see if the handler is there i cannot find it

我正确附加了处理程序吗?

Am I attaching the handler correctly?

任何建议

     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);
        }
    }

推荐答案

您显示的代码是推荐的方法.

The code you have shown is the recommend approach.

关于_gitHubClient

//我的_gitHubClient没有附加的处理程序!!!

//my _gitHubClient has no Handler attached!!!

似乎是一种误解.

_gitHubClient是将HttpClient实例包装在其GitHubClient实现中的抽象.

_gitHubClient is your abstraction that is wrapping a HttpClient instance in its GitHubClient implementation.

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("/");
    }
}

包裹的实例将具有附加的处理程序.

It is that wrapped instance that would have the attached handler.

基于当前配置,每当框架必须创建派生的IGitHubClient实例以进行注入时,工厂将使用启动时提供的设置来创建HttpClient.其中还包括添加ConfigurePrimaryHttpMessageHandler

Based on the current configuration, whenever the framework has to create an instance of the IGitHubClient derived GitHubClient for injection, the factory will create a HttpClient using the settings provided at start up. Which would also include adding the HttpClientHandler provided by ConfigurePrimaryHttpMessageHandler

这篇关于将httpHandler附加到httpclientFactory webapi aspnetcore 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆