47

I have a .NET 4.6.2 console application (using Simple Injector). I need to make calls to an HTTP service. Having run into issues using HttpClient directly, I'm trying to use HttpClientFactory ( https://github.com/aspnet/HttpClientFactory ) instead.

The project/library is .NET Standard 2.0 so it should?? work in .NET 4.6.2, but it uses stuff like IServiceCollection, which is in Core only.

So my question is can I use HttpClientFactory in a non-Core application.

Test45
  • 563
  • 1
  • 5
  • 12
  • 1
    `IServiceCollection` is also not core only. – Nkosi Jul 24 '18 at 12:46
  • https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection?view=aspnetcore-2.1 says ASP.NET Core 2.1, 2.0, 1.1, 1.0 – Test45 Jul 24 '18 at 13:46
  • 1
    Look at the Nuget package and you will see https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/2.1.1 – Nkosi Jul 24 '18 at 13:49

2 Answers2

54

All of the Microsoft.Extensions.* packages target .NET Standard 2.0. That means that you can use Dependency Injection, Configuration, Logging and HttpClientFactory if you add the appropriate package to your application.

You need to add Microsoft.Extensions.Http to use HttpClientFactory and Microsoft.Extensions.Http.Polly if you want to use it with Polly

To configure HttpClientFactory you'll have to add Microsoft.Extensions.DependencyInjection to your "main" project. Microsoft.Extensions.Http package only depends on Microsoft.Extensions.DependencyInjection.Abstractions which contains the interfaces, not the DI provider itself.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 10
    Some of my web service projects are already using Ninject as DI provider. Is it possible to use the HttpClientFactory in this scenario as well? Is there any other mechanism for using HttpClientFactory with Ninject? PS: My web services are not in .NET core but using .NET 4.7.* – Vinod Jan 18 '19 at 14:23
  • 6
    Is there any sample code in terms of the configuration? – Nico Jan 22 '19 at 02:13
  • 1
    @Vinod we do the same thing, we use a `new ServiceCollection` and use all the extensions methods on that to bind all the things, then have a `INinjectHttpClientFactory` that uses the `IServiceCollection` service provider to actually build things. – tigerswithguitars Sep 23 '19 at 13:49
  • Is it possible to do it inside Ninject module? – zolty13 Dec 09 '19 at 09:29
36

As @Panagiotis Kanavos said, you need to add
Microsoft.Extensions.Http and Microsoft.Extensions.DependencyInjection.
Here is my ConsoleApp code, you can refer to it.

class Program
{
     static void Main(string[] args)
     {
         Test();
         Console.Read();
     }

     static async void Test()
     {
        var services = new ServiceCollection();
        //services.AddSingleton<xx,xx>();
        //...
        services.AddHttpClient();
        var serviceProvider = services.BuildServiceProvider();
        
        var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
        var client = httpClientFactory.CreateClient();
        var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://www.baidu.com"));
        var content = await response.Content.ReadAsStringAsync();
        Console.WriteLine(content);
     }
}
Jim
  • 489
  • 5
  • 11
  • If I loop the Test(), will it created multiple instances of client? – Gopi Apr 01 '19 at 11:34
  • @Gopi yes, but if you register serviceProvider outstide off this method then every `var client = httpClientFactory.CreateClient();` will return the same client – bombek Mar 26 '20 at 12:18
  • HttpClient can be created multiple times it does not matter. HttpMessageHandler will be reused, as long as you use the same instance of the factory, which is the important part here. – Dogu Arslan Oct 22 '21 at 11:15