简体   繁体   English

尝试激活服务时无法解析类型“System.Lazy`1[System.Net.Http.IHttpClientFactory]”的服务

[英]Unable to resolve service for type 'System.Lazy`1[System.Net.Http.IHttpClientFactory]' while attempting to activate service

I am getting the below error while trying to inject Lazy.尝试注入 Lazy 时出现以下错误。

It works fine without "Lazy<>"没有“Lazy<>”也能正常工作

I have registered it like below in startup.cs.我已经在startup.cs中注册了它,如下所示。

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpClient();
        ..

        ..
   }

I am trying to inject it in a controller like below:我正在尝试将它注入到如下所示的控制器中:

 private readonly Lazy<IHttpClientFactory> _clientFactory;
    protected IHttpClientFactory ClientFactory => _clientFactory.Value;

    public ValuesController(Lazy<IHttpClientFactory> clientFactory)
    {
         _clientFactory = clientFactory;
    }

Here, I am getting an error:在这里,我收到一个错误:

System.InvalidOperationException: 'Unable to resolve service for type 'System.Lazy`1[System.Net.Http.IHttpClientFactory]' while attempting to activate 'POC.Core.Controllers.ValuesController'.'

Any idea, what could be the issue in lazy initialization?任何想法,延迟初始化可能是什么问题?

Just don't use Lazy<IHttpClientFactory> .只是不要使用Lazy<IHttpClientFactory> HttpClientFactory itself is the type that creates and caches instances of HttpClients, or rather, HttpClientHandlers. HttpClientFactory 本身是创建和缓存 HttpClients 或 HttpClientHandlers 实例的类型。

It's a singleton managed by the DI container itself so Lazy<IHttpClientFactory> wouldn't have any effect, even if you got it to compile.它是一个由 DI 容器本身管理的单例,因此Lazy<IHttpClientFactory>不会产生任何影响,即使您编译它也是如此。

The source code of AddHttpClient explicitly registers a default HttpClientFactory as a singleton. AddHttpClient的源代码显式地将默认 HttpClientFactory 注册为单例。

    public static IServiceCollection AddHttpClient(this IServiceCollection services)
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }

        services.AddLogging();
        services.AddOptions();

        //
        // Core abstractions
        //
        services.TryAddTransient<HttpMessageHandlerBuilder, DefaultHttpMessageHandlerBuilder>();
        services.TryAddSingleton<DefaultHttpClientFactory>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有“System.Net.Http.IHttpClientFactory”类型的注册服务 - There is no registered service of type 'System.Net.Http.IHttpClientFactory 尝试激活时无法解析类型“System.Net.Http.HttpClient”的服务 - Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate System.InvalidOperationException:尝试激活时无法解析服务类型 - System.InvalidOperationException: Unable to resolve service for type while attempting to activate .NET Core 依赖注入; 尝试激活服务时无法解析“System.String”类型的服务 - .NET Core Dependency Injection; Unable to resolve service for type 'System.String' while attempting to activate Service ASP.NET Core Web API - 无法为服务类型实例化实现类型“System.Net.Http.IHttpClientFactory” - ASP.NET Core Web API - Cannot instantiate implementation type 'System.Net.Http.IHttpClientFactory' for service type ASP.NET 核心错误:System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Core error: System.InvalidOperationException: Unable to resolve service for type while attempting to activate ASP.NET 核心 - System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Core - System.InvalidOperationException: Unable to resolve service for type while attempting to activate ASP.NET 错误:内部服务器错误:System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Error: Internal Server Error: System.InvalidOperationException: Unable to resolve service for type while attempting to activate 尝试激活时无法解析类型为&#39;System.Int32&#39;的服务 - Unable to resolve service for type 'System.Int32' while attempting to activate System.InvalidOperationException:尝试激活 Controller 时无法解析“ChatBotDashboard.ApplicationDbContext”类型的服务 - System.InvalidOperationException: Unable to resolve service for type 'ChatBotDashboard.ApplicationDbContext' while attempting to activate Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM