Skip to content

Instantly share code, notes, and snippets.

@akhansari
Last active October 19, 2022 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhansari/ce50ff96b86fbaa26868c9005f654427 to your computer and use it in GitHub Desktop.
Save akhansari/ce50ff96b86fbaa26868c9005f654427 to your computer and use it in GitHub Desktop.
HttpClientFactory and named HttpClient with F#
#r "nuget: Microsoft.Extensions.Http, 6.0.0"
let factory =
ServiceCollection()
.AddHttpClient("dummy")
.ConfigureHttpClient(fun c ->
printfn "dummy configured"
c.BaseAddress <- Uri "https://dummyjson.com"
c.Timeout <- TimeSpan.FromMinutes 2.)
.Services
.BuildServiceProvider()
.GetService<IHttpClientFactory>()
(*
There is no need to dispose of the HttpClient instances from HttpClientFactory.
Disposal will not actually do anything in this case
because the factory manages the handler and connection lifetimes and not the HttpClient instances.
*)
let fetchPlaceHolder () =
let client = factory.CreateClient()
client.GetStringAsync "https://jsonplaceholder.typicode.com/todos/1"
let fetchDummy () =
let client = factory.CreateClient "dummy"
client.GetStringAsync "/todos/1"
fetchDummy().Result |> printfn "%s\n"
fetchPlaceHolder().Result |> printfn "%s\n"
fetchDummy().Result |> printfn "%s\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment