How to add multiple HttpMessageHandler to HttpClient without HttpClientFactory

DelegatingHandler has a protected constructor that takes a handler for the inner handler. If you have control over all your custom handlers, I would think you can add a public constructor that calls the protected constructor, like:

public class CustomHandler : DelegatingHandler
{
    public CustomHandler(HttpMessageHandler innerHandler) : base(innerHandler)
    {
    }
}

and chain them thus:

var client = new HttpClient(
    new CustomHandler(
        new OtherCustomerHandler(
            new HttpClientHandler()
        )
    )
);

I think you can just do something like this:

var loggingHandler = new LoggingHandler();
var responseContentProcessingHandler =  new ResponseContentProcessingHandler();
loggingHandler.InnerHandler = responseContentProcessingHandler;
var client = new HttpClient(loggingHandler);

So that you don't need to create a CustomHandler just for the chaining purpose. That's really the purpose of DelegatingHandler.


To Achieve with Console application on .Net 4.0 platform,best possible solution is to pass httpConfiguration to your webapi library or if you dont hold WebAPI code just write this code in global.ascx file of webhost asp.net application

     protected void Application_Start(object sender, EventArgs e)
             {
        var config = GlobalConfiguration.Configuration;
        WebAPIConfig.Configure(config);


    }

   //Code that will configure all message handlers in webapi    
          public static void Configure(HttpConfiguration configure)
   {


        configure.MessageHandlers.Add(new xyzhandler());
       configure.MessageHandlers.Add(new ABCHandler());

   }

In your Console Application,place uri of your webhost hosting webapi

   private const string APiUri="http://localhost/api/get"
   using(HttpClient cleint=new HttpClient()){
     var response await cleint.GetAsync(ApiUri);
    ....
               }