9

I have ASP.NET Core 2.1 project which uses a class library built on .Net Core 2.1. In .Net Core 2.1 class library i need to make several REST class,for which i am using HttpClient class.

I want to leverage the benefits of HttpClientFactory for creating and using HttpClient instances.

How can i use HttpClientFactory in .Net Core 2.1 class library.

user2550231
  • 111
  • 3
  • Are you certain that you actually need to use `HttpClientFactory`? If the class library you're using only creates a few, long-lived instances of `HttpClient` then you don't need to use `HttpClientFactory`. If you do need to use it, then you would pass it in as a DI parameter to some "service" class object instance instantiated in ASP.NET Core's own DI container. – Dai Mar 08 '19 at 07:26
  • Idea to use HttpClientFactory was to make use of the functionality available for Retry and Fault Handling. – user2550231 Mar 08 '19 at 08:33
  • The http instances used in class libraries are not long lived,and i need to make call to different rest endpoints often. – user2550231 Mar 08 '19 at 08:37
  • 1
    There is a pretty good article for the same. https://www.talkingdotnet.com/3-ways-to-use-httpclientfactory-in-asp-net-core-2-1/ – mukesh joshi Mar 08 '19 at 13:07
  • More available documentation: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1#use-polly-based-handlers ; https://www.stevejgordon.co.uk/httpclientfactory-using-polly-for-transient-fault-handling ; https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory – mountain traveller Mar 08 '19 at 17:33
  • 3
    Thanks for the info,But all these describes about using HttpClienyFactory in Asp.Net core 2.1.but how to use thisin .Net Core 2.1 class library – user2550231 Mar 09 '19 at 09:16
  • You can find some useful information here: https://stackoverflow.com/a/52622851/862795 – Rzassar Aug 11 '20 at 08:46

0 Answers0