Question: HttpClientFactory custom http message handlers

未关闭
#360 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Wolffy101 已经在做这个了。

开始于 2026年10月9日。

  • #574 来自 @Wolffy101 —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp

调研方向

首先阅读 HttpClientFactory.CreateHandler(CreateHttpClientArgs args) 以及 issue 中展示的 IHttpMessageHandlerFactory 用法,然后检查现有的 SDK 注册和初始化流程。当多个由 DI 解析的 handler 可以在不丢失 gzip、user-agent 配置或其他 SDK 初始化器的情况下复用时,即视为完成;未指定任何文件或测试路径。

由索引模型根据 Issue 内容生成。

描述

needs-triage

Hello,
do you have an example, how we can extend http client factory to add multiple http message handlers and keep current implementation of google http client factory? In essence, I have multiple handlers covering cross cutting concerns (traces, metrics, logs) which I am using with other typed http clients, I would like them to be reused. Ideally it would the best to resolve them from DI.

I have found similar issue in other library which uses google http client factory ref: https://github.com/googleapis/google-api-dotnet-client/issues/1756, however some of the implementation is missing in current.

For now I have implemented as such (since I could not find any other possible way)

// configure handlers, so I could resolve through IHttpMessageHandlerFactory
builder.Services.AddHttpClient("Firebase")
    .ConfigurePrimaryHttpMessageHandler(_ => // some proxy configuration)
    .AddHttpMessageHandler<CorrelationIdHandler>()
    .AddHttpMessageHandler<LoggingHandler>()
    .AddHttpMessageHandler<TracingHandler>();

// override google http client factory
public class FirebaseHttpClientFactory : HttpClientFactory
{
      private readonly IHttpMessageHandlerFactory _httpMessageHandlerFactory;

      public FirebaseHttpClientFactory(IHttpMessageHandlerFactory httpMessageHandlerFactory)
      {
             _httpMessageHandlerFactory = httpMessageHandlerFactory
      }

      protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
      {
             return _httpMessageHandlerFactory.CreateHandler("Firebase");
      }
}

// register firebase messaging 
builder.Services.AddSingleton(sp =>  return FirebaseMessaging.GetMessaging(FirebaseApp.Create(new AppOptions{
     Credential = // load from file
     HttpClientFactory = new FirebaseHttpClientFactory(sp.GetRequiredService<IHttpMessageHandlerFactory>())
}));

By doing this, it kinda works, however I loose a lot of functionality from SDK itself, like gzip, user agent name setter and other initializers.

Can you please confirm that this is the only way currently how to achieve this and whether this will work?

I can see that other google library ref: https://github.com/googleapis/google-api-dotnet-client/issues/1756 already decoupled itself from accepting implementation of google.HttpClientFactory to interface (what let's to use DI and pass multiple handlers).

Are you planning to make similar update to this library? If yes and you need help, please let me know :)

主要语言
C#
星标
407
派生
147
平均合并
2 天 10 小时
30 天内合并 PR
12

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

firebase/firebase-admin-dotnet 的其他 Issue

查看 firebase/firebase-admin-dotnet 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。