本文介绍了Autofac无法解析可枚举的类型HttpClients的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多服务,需要使用HttpClientFactory中键入的HttpClient.尽管我可以解析一项服务,但我无法解析该服务的IEnumerable.

I have a number of services which require usage of typed HttpClient from HttpClientFactory.Though I can resolve one service I can't resolve IEnumerable of this services.

interface IMyHttpClient
{
}

class MyHttpClient: IMyHttpClient
{
    public MyHttpClient(HttpClient client)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        var services = new ServiceCollection();
        services.AddHttpClient()
                .AddHttpClient<IMyHttpClient, MyHttpClient>();

        var builder = new ContainerBuilder();
        // Exception goes away when remove this line
        builder.RegisterType<MyHttpClient>().As<IMyHttpClient>();
        builder.Populate(services);
        var provider = builder.Build();

        // ============== This works
        // provider.Resolve<IMyHttpClient>();

        // ============== This throws exception
        provider.Resolve<IEnumerable<IMyHttpClient>>();
    }
}

构造函数将被调用一次,然后抛出异常:

Constructor will be called once and than exception is thrown:

```DependencyResolutionException:在类型为"ConsoleApp2.MyHttpClient"的"Autofac.Core.Activators.Reflection.DefaultConstructorFinder"中找不到任何构造函数,都不能使用可用的服务和参数来调用:无法解析构造函数"Void .ctor(System.Net.Http.HttpClient)"的参数"System.Net.Http.HttpClient客户端".

```DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ConsoleApp2.MyHttpClient' can be invoked with the available services and parameters:Cannot resolve parameter 'System.Net.Http.HttpClient client' of constructor 'Void .ctor(System.Net.Http.HttpClient)'.

```

问题是AddHttpClient添加了它自己的IMyHttpClient注册.但是我只想使用Autofac进行注册!有没有办法使用类型化的客户端,但仍然使用Autofac?

The issue is that AddHttpClient adds it's own registration of IMyHttpClient. But I do want to register using Autofac only! Is there a way to use typed clients but still stay with Autofac?

推荐答案

该异常说明Autofac无法解析参数"System.Net.Http.HttpClient客户端".我想这是因为没有为第二次注册IMyHttpClient在您的容器中注册这种类型.为了节省HttpClientFactory的优点,您可以像下面这样注册示例构造函数参数:

The exception explains that Autofac can't resolve parameter 'System.Net.Http.HttpClient client'. I suppose this is because such type wasn't registered in your container for second resgistration of IMyHttpClient. To save an advantages of HttpClientFactory you can register excplicit constructor parameter for example like this:

static void Main(string[] args)
{
    var services = new ServiceCollection();
    services.AddHttpClient();

    var builder = new ContainerBuilder();
    // exclicit resolving client for constructor
    builder.RegisterType<MyHttpClient>().As<IMyHttpClient>().WithParameter(
        (p, ctx) => p.ParameterType == typeof(HttpClient),
        (p, ctx) => ctx.Resolve<IHttpClientFactory>().CreateClient());

    builder.Populate(services);
    var provider = builder.Build();

    // ============== This works
    provider.Resolve<IMyHttpClient>();

    // ============== This works too
    provider.Resolve<IEnumerable<IMyHttpClient>>();
}

在此示例中,Resolve<IEnumerable<IMyHttpClient>>返回带有单个IMyHttpClient的枚举,该枚举由核心HttpClientFactory中的HttpClient初始化.

In this example Resolve<IEnumerable<IMyHttpClient>> return enumeration with single IMyHttpClient, that is initialized with HttpClient from core HttpClientFactory.

UPD:该帖子已通过@norekhov评论

UPD: the post was updated by @norekhov comment

这篇关于Autofac无法解析可枚举的类型HttpClients的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:47