IHttpClientFactory without ASP.NET

Sometimes you don't want to use ASP.NET's Dependency Injection, in some of these cases, you can get away by using other forms of dependency injection (specially in F#). However, one of the things that I always missed was the IHttpClientFactory interface.

The HttpClient injected by ASP.NET manages a lot for you, ranging from DNS changes to thread-safety. Here's how you can use without ASP.NET's services:

open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection

let builder = Host.CreateApplicationBuilder()

let serviceProvider = ServiceCollection().AddHttpClient().BuildServiceProvider();
let factory = serviceProvider.GetService<IHttpClientFactory>();

// usage example
let client = factory.CreateClient()