0

I've implemented the methodology outlined by Xamgirl on this post:

https://xamgirl.com/consuming-restful-web-service-xamarin-forms-using-refit-part-3/

If you look at the code she's instantiating 2 ApiServices for the MakeUpApi and RedditApi and thus 2 HttpClient instances.

HttpClient is designed to be used as a single instance so I'm wondering if anyone knows how to do this bearing in mind the Fusillade priority could vary according to each request.

Thanks in advance

ledragon
  • 299
  • 6
  • 17
  • `HttpRequestHandler` is designed to be reused not the Client itself. You could instantiate both the HttpClients with the same handler. Also, AFAIK, reuse makes sense only if you have the same base url. If you are calling two different apis, using same client/handler doesn't give any advantage. – memsranga Jun 04 '19 at 12:28
  • @shanranm Hi Thanks for the reply...do you mean HttpMessageHandler rather than HttpRequestHandler? I'm using the same base Uri for all request BTW – ledragon Jun 04 '19 at 13:52
  • Yes, HttpMessageHandler – memsranga Jun 04 '19 at 14:37
  • FYI Answer here: https://stackoverflow.com/questions/63803919/addrefitclient-dryioc-and-iserviceprovider-on-prism-for-xamarin-forms/68068850#68068850 – ledragon Jun 21 '21 at 13:52

1 Answers1

3

HttpClient is intended to be instantiated once and reused throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. That issue will result in SocketException errors. Possible approaches to solve that problem are based on the creation of the HttpClient object as singleton or static.

But there’s a another issue with HttpClient that you can have when you use it as singleton or static object. In this case, a singleton or static HttpClient doesn't respect DNS changes, as explained in this issue at the .NET Core GitHub repo.

To address those mentioned issues and make the management of HttpClient instances easier, .NET Core 2.1 introduced a new HttpClientFactory that can also be used to implement resilient HTTP calls by integrating Polly with it.

More detailed info, please take a look:

https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16