Question: HttpClientFactory custom http message handlers

Aperta
#360 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Wolffy101 ci sta già lavorando.

Dal 9/10/2026.

  • #574 di @Wolffy101 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
csharp

Direzione di ricerca

Inizia leggendo HttpClientFactory.CreateHandler(CreateHttpClientArgs args) e l’utilizzo di IHttpMessageHandlerFactory mostrato nell’issue, quindi esamina la registrazione esistente dell’SDK e il flusso di inizializzazione. Il lavoro è completato quando è possibile riutilizzare più handler risolti da DI senza perdere gzip, la configurazione di user-agent o altri inizializzatori dell’SDK; non viene indicato alcun percorso di file o test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 :)

Lingua principale
C#
Stelle
408
Fork
147
Merge medio
1g 3h
PR unite (30g)
10

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di firebase/firebase-admin-dotnet

Tutte le issue di firebase/firebase-admin-dotnet

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.