Last active
October 19, 2022 08:51
-
-
Save akhansari/ce50ff96b86fbaa26868c9005f654427 to your computer and use it in GitHub Desktop.
HttpClientFactory and named HttpClient with F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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