如何使用autofac注册键入的httpClient服务?

编程入门 行业动态 更新时间:2024-10-26 18:30:12
本文介绍了如何使用autofac注册键入的httpClient服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建MVC Web应用程序,该应用程序使用 core 2.2使用单独的 HttpClient s调用api来调用每个控制器(相同的api)。

I'm creating MVC web application which calls an api using core 2.2 using separate HttpClients to call each controller (same api).

例如:

  • 对于用户控制器操作:UserService(httpclient)
  • 对于后控制器操作:PostService(httpclient)

在 startup.cs 我将DI用作:

In startup.cs I use DI as:

services.AddHttpClient<IUserService, UserService>(); services.AddHttpClient<IPostService, PostService>();

在我的处理程序中:

public class CommandHandler : IRequestHandler<Command, BaseResponse> { private readonly IUserService _userService; public CommandHandler(IUserService userService) { _userService = userService; } public Task<BaseResponse> Handle(Command request, CancellationToken cancellationToken) { throw new System.NotImplementedException(); } }

但是调用命令处理程序时出现此错误:

But when invoking command handler I get this error:

找不到与'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'相同的构造函数,类型为'xxx可以使用调用可用的服务和参数来调用.Application.Services.Users.UserService:无法解析构造函数'Void .ctor的参数'System.Net.Http.HttpClient httpClient' (System.Net.Http.HttpClient, xxx.Application.Configurations.IApplicationConfigurations, Microsoft.Extensions.Logging.ILogger`1 [xxx.Application.Services.Users.UserService])。

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'xxx.Application.Services.Users.UserService' can be invoked with the available services and parameters: Cannot resolve parameter 'System.Net.Http.HttpClient httpClient' of constructor 'Void .ctor(System.Net.Http.HttpClient, xxx.Application.Configurations.IApplicationConfigurations, Microsoft.Extensions.Logging.ILogger`1[xxx.Application.Services.Users.UserService])'.

但是我已经在autofac模块中注册了服务:

But I've registered services in autofac module:

public class ServiceModule : Module { protected override void Load(ContainerBuilder builder) { builder.RegisterAssemblyTypes(typeof(ServiceModule).Assembly) .Where(t => t.Namespace.StartsWith("xxx.Application.Services")) .AsImplementedInterfaces().InstancePerLifetimeScope(); } }

这是我的 UserService 类构造函数:

public UserService (HttpClient httpClient, IApplicationConfigurations applicationConfig, ILogger<UserService> logger) { _httpClient = httpClient; _applicationConfig = applicationConfig; _logger = logger; _remoteServiceBaseUrl = $"{_applicationConfig.WebApiBaseUrl}"; }

我有两个问题:

  • 上述错误是什么意思?
  • 在api中为不同的控制器使用单独的httpclient是好习惯吗?
  • 推荐答案

    通过

    services.AddHttpClient<IUserService, UserService>();

    您将配置本机核心依赖项注入以注入 HttpClient 转换为 UserService 。

    You will configure the native core dependency injection to inject HttpClient to UserService when a IUserService is requested.

    然后您完成

    builder.RegisterAssemblyTypes(typeof(ServiceModule).Assembly) .Where(t => t.Namespace.StartsWith("xxx.Application.Services")) .AsImplementedInterfaces().InstancePerLifetimeScope();

    这将删除 IUserService 。 IUserService 现在已在 UserService 中注册,其中没有任何 HttpClient 心神。

    which will erase the native dependency injection configuration for IUserService. The IUserService is now registered with UserService without any HttpClient in mind.

    添加 HttpClient 的最简单方法是像这样注册它:

    The simplest way to add HttpClient would be to register it like this :

    builder.Register(c => new HttpClient()) .As<HttpClient>();

    services.AddHttpClient(); // register the core IHttpClientFactory builder.Register(c => c.Resolve<IHttpClientFactory>().CreateClient()) .As<HttpClient>();

    如果要为特定服务配置httpclient,可以创建一个autofac模块,该模块添加如下参数

    If you want to configure your httpclient for a specific service you can create an autofac module which add parameters like this :

    public class HttpClientModule<TService> : Module { public HttpClientModule(Action<HttpClient> clientConfigurator) { this._clientConfigurator = clientConfigurator; } private readonly Action<HttpClient> _clientConfigurator; protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) { base.AttachToComponentRegistration(componentRegistry, registration); if (registration.Activator.LimitType == typeof(TService)) { registration.Preparing += (sender, e) => { e.Parameters = e.Parameters.Union( new[] { new ResolvedParameter( (p, i) => p.ParameterType == typeof(HttpClient), (p, i) => { HttpClient client = i.Resolve<IHttpClientFactory>().CreateClient(); this._clientConfigurator(client); return client; } ) }); }; } } }

    然后

    builder.RegisterModule(new HttpClientModule<UserService>(client => { client.BaseAddress = new Uri("api.XXX/"); client.DefaultRequestHeaders.Add("Accept", "application/vnd.XXX.v3+json"); client.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-XXX"); }));

    更多推荐

    如何使用autofac注册键入的httpClient服务?

    本文发布于:2023-11-05 21:17:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1561887.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何使用   autofac   httpClient

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!