3.33Setting the HttpClient instance provider
Created on Aug 17, 2026~2 min read
By default, the system creates and automatically manages the lifetime of HttpClient instances through IHttpClientFactory. If you need to manage the HttpClient lifetime manually, you can configure a separate HttpClient instance for a single request.
HttpRequestBuilder.Get("https://furion.net/") .SetHttpClientProvider(() => (new HttpClient(), client => client.Dispose()));SetHttpClientProvider method description:
- The parameter type is the
Func<(HttpClient, Action<HttpClient>?)>delegate. - This delegate returns a tuple, where the first element is the
HttpClientinstance used to send the request. - The second element (optional) is a delegate used to dispose the
HttpClientinstance after the request completes.
Before the request is sent, the system calls this delegate (if it exists) and obtains the HttpClient instance to make the request; after the request completes, if a disposal delegate was provided, it is called to dispose the HttpClient instance.