Innovator.Client icon indicating copy to clipboard operation
Innovator.Client copied to clipboard

HttpClientHandler manipulation

Open gratinierer opened this issue 6 years ago • 0 comments

In some cases it is necessary to adjust the HttpClientHandler that is used by HttpClient (e.g. using certificates, configure SSL etc.). After walking through the code for a while I recognized that the HttpClient seems to be replaceable via ConnectionPreferences via

public HttpClient HttpService { get; set; }

Fine at first glance, but

  1. this only sets one instance of an client. So looking at
      var masterService = preferences.HttpService ?? DefaultService.Invoke();
      var arasSerice = preferences.HttpService ?? DefaultService.Invoke();

shows that two separate clients are build, but if you set it via preferences there is only one client. I'm not sure if everything still works with a single instance. 2. There are places where the DefaultService is used, even if you set another client via preferences: public static IPromise<Stream> AsFile(this IReadOnlyProperty prop, IConnection conn, bool async) seems go always via the default client.

So my next idea was to implement a

    public Action<HttpClientHandler> HttpClientHandlerEnricher
    {
      get => _httpClientHandlerEnricher ?? (h => { });
      set => _httpClientHandlerEnricher = HttpClientHandlerEnricher;
    }

This Action could be part of the ConnectionPreferences and then be called on the handlers. In first place this had to happed in the Factory itself, because (1.) and (2.), r.g.

    static Factory()
    {
      DefaultService = () =>
      {
        var handler = new SyncClientHandler
        {
          CookieContainer = new CookieContainer()
        };
        ConnectionPreferences.HttpClientHandlerEnricher(handler);

Unfortunately the static initializer avoids that any ConnectionPreferences are set at that moment.

So I don't have a good idea of placing the handler-manipulating code. But maybe I'm missing something and there is already a solution for that.

gratinierer avatar Dec 11 '19 14:12 gratinierer