依赖注入:HttpClient还是HttpClientFactory? [英] Dependency injection: HttpClient or HttpClientFactory?

查看:673
本文介绍了依赖注入:HttpClient还是HttpClientFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何地方,我都能看到三种在DI中创建客户端(基本,命名,类型)的主要方法,但是我找不到注入IHttpClientFactoryHttpClient的方法(两者都可行).

Everywhere I can see three main approaches to create clients (basic, named, typed) in DI, but I have found nowhere if to inject IHttpClientFactory or HttpClient (both possible).

问题1 :请注入IHttpClientFactoryHttpClient有什么区别?

Q1: What is the difference between injecting IHttpClientFactory or HttpClient please?

第二季度:而且如果注入了IHttpClientFactory,我应该为每个呼叫使用factory.CreateClient()吗?

Q2: And if IHttpClientFactory is injected, should I use factory.CreateClient() for each call?

推荐答案

摘要

  • HttpClient只能注入已键入的客户端
  • 其他用途,您需要IHttpClientFactory
  • 在两种情况下,HttpClientMessageHandler的生存期都由框架管理,因此您不必担心(错误地)处置HttpClients.
  • Summary

    • HttpClient can only be injected inside Typed clients
    • for other usages, you need IHttpClientFactory
    • In both scenarios, the lifetime of HttpClientMessageHandler is managed by the framework, so you are not worried about (incorrectly) disposing the HttpClients.
    • 要直接注入HttpClient,您需要注册将接收客户端的特定 Typed 服务:

      In order to directly inject HttpClient, you need to register a specific Typed service that will receive the client:

      services.AddHttpClient<GithubClient>(c => c.BaseAddress = new System.Uri("https://api.github.com"));
      

      现在,我们可以将其注入键入 GithubClient

      Now we can inject that inside the typed GithubClient

      public class GithubClient
      {
          public GithubClient(HttpClient client)
          {
              // client.BaseAddress is "https://api.github.com"
          }
      }
      

      您不能将HttpClient插入到AnotherClient内,因为它没有在AnotherClient

      You can't inject the HttpClient inside AnotherClient, because it is not typed to AnotherClient

      public class AnotherClient
      {
          public AnotherClient(HttpClient client)
          {
              // InvalidOperationException, can't resolve HttpClient 
          }
      }
      

      但是,您可以:
      1.注入IHttpClientFactory并调用CreateClient().此客户端会将BaseAddress设置为null.
      2.或将AnotherClient配置为其他类型的客户端,例如,使用不同的BaseAdress.

      You can, however:
      1. Inject the IHttpClientFactory and call CreateClient(). This client will have BaseAddress set to null.
      2. Or configure AnotherClient as a different typed client with, for example, a different BaseAdress.

      根据您的评论,您正在注册命名客户端.仍然可以通过IHttpClientFactory.CreateClient()方法进行解析,但是您需要传递客户端的名称"

      Based on your comment, you are registering a Named client. It is still resolved from the IHttpClientFactory.CreateClient() method, but you need to pass the 'name' of the client

      注册

      services.AddHttpClient("githubClient", c => c.BaseAddress = new System.Uri("https://api.github.com"));
      

      用法

      // note that we inject IHttpClientFactory
      public HomeController(IHttpClientFactory factory)
      {
          this.defaultClient = factory.CreateClient(); // BaseAddress: null
          this.namedClient = factory.CreateClient("githubClient"); // BaseAddress: "https://api.github.com"
      }
      

      这篇关于依赖注入:HttpClient还是HttpClientFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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