通过IHttpClientFactory在DelegatingHandler中注入值

时间:2018-08-22 07:47:53

标签: c# dependency-injection

在为IHttpClientBuilder构建自定义中间件时,请使用DelegatingHandler和IHttpClientFactory。

我的类的构造函数如下所示。

  public HttpApiFactory(HttpApifactoryConfig config, IHttpClientFactory httpClientFactory, IServiceProvider serviceProvider)
    {
        this.httpClientFactory = httpClientFactory;
        this.serviceProvider = serviceProvider;
        this.config = config;
    }

这就是所谓的方法

public IApiClient CreateClient(MethodInfo methodInfo, object param, string pipelineName)
    {
        return new Simployer.Service.Client.HttpApiClient(
        httpClientFactory.CreateClient(pipelineName), config.serializer);

    }

我知道将要实例化的一个注册处理程序需要给定对象的实例。此类型未在IServiceCollection中注册,因为我无法在启动时或在工厂中实例化该类。

我想在调用httpClientFactory.CreateClient()之前以某种方式将“ methodInfo”参数添加到IServiceContainer,IServiceProvider中 我在想IServiceScope,但是找不到插入实例的方法。

1 个答案:

答案 0 :(得分:0)

我通过更改构造函数以接受能够为我获取值的工厂来解决了这一问题。