Can I use HttpClientFactory in a .NET.core app which is not ASP.NET Core?

前端 未结 4 583
悲&欢浪女
悲&欢浪女 2021-01-07 21:45

I have read the popular blog post https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore on using HttpClientFactory

To quote from it

相关标签:
4条回答
  • 2021-01-07 22:35

    Thanks for replies.

    So it is possible to use in console app.

    There are a few ways to do this, depending on what way you want to go. Here are 2:

    1. Directly add to ServiceCollection e.g. services.AddHttpClient()

    2. Use Generic host e.g. Add httpclientFactory in .ConfigureServices() method

    See here for blog post using in console app

    0 讨论(0)
  • 2021-01-07 22:36

    As one of the answers suggests,

    you do not need ASP.NET to use it

    However, you need a bit of work to get it into your Dependency Injection (DI):

    • Install microsoft.extensions.http (has nothing to do with ASP.NET)

    • When configuring your DI, use this extension. it registers builders/httpclientFactory/... (have a look at its source code on github)

      ServiceCollections.AddHttpClient();
      
    • if you want register HttpClient with different names/settings to communicate with different web servers (different settings, ex: different base urls)

      ServiceCollection.AddHttpClient(
      "yourClientName", x => x.BaseAddress = new Uri("http://www.mywebserver.com"))
      
    • In case you want to add DelegateHendlers, you need to add it both to your httpClient and your DI container.

      ServiceCollection
              .AddHttpClient(clientName, x => x.BaseAddress = new Uri("http://www.google.com"))
              .AddHttpMessageHandler<DummyDelegateHandler>();
      ServiceCollection.AddScoped<DummyDelegateHandler>();
      
    • register your HttpClient to use HttpClientFactory

      ServiceCollection.AddScoped<HttpClient>(x => 
      x.GetService<IHttpClientFactory>().CreateClient("yourClientName"));
      
    • To resolve http client:

      var client = ServiceProvider.GetService<HttpClient>();
      
    0 讨论(0)
  • 2021-01-07 22:40

    Just providing sample code for the first suggested approach in the accepted answer:

    services.AddHttpClient<IFoo, Foo>(); // where services is of type IServiceCollection
    

    and your class looks like:

    public class Foo : IFoo
    {
        private readonly HttpClient httpClient;
    
        public Consumer(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 22:41

    According to the documentation HttpClientFactory is a part of .Net Core 2.1, so you don't need ASP.NET to use it. And there some ways to use are described. The easiest way would be to use Microsoft.Extensions.DependencyInjection with AddHttpClient extension method.

    static void Main(string[] args)
    {
        var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
    
        var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
    
        var client = httpClientFactory.CreateClient();
    }
    
    0 讨论(0)
提交回复
热议问题