Using DelegationHandler for HTTP Requests

sonal khatri 46 Reputation points

Hi, I have a requirement to use a DelegationHandler. I have a ServiceA class and an AuthHandler class that inherits from the DelegationHandler class. I created a RestClient class which has an HTTPClient injected, and in this class, I have the common logic to call HttpClient methods, set queryParameters, set headers, etc. My ServiceA class is injecting the RestClient class and, in turn, calls RestClient methods, which, in turn, make all the HTTP requests. I have implemented it like this as I want to keep a generic RestClient class so that all my services, like ServiceA, ServiceB, can call this class, and I do not need to repeat common code. But if I follow this approach, then I am not able to use the DelegationHandler, and it is not invoked. Can you suggest a way?

A set of technologies in the .NET Framework for building web applications and XML web services.
4,023 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,978 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce ( 52,826 Reputation points

    as the HttpClientFactory implements the delegate pipeline for httpclient, instead of injecting HttpClient, you should inject HttpClientFactory, or use named or typed HttpClients (which implicitly use the factory).

    0 comments No comments