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

前端 未结 4 584
悲&欢浪女
悲&欢浪女 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: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();
    
        var client = httpClientFactory.CreateClient();
    }
    

提交回复
热议问题