Add HttpClient factory support for KookSocketConfig#41
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds HttpClient factory support to enable integration with IHttpClientFactory from dependency injection frameworks. The changes allow users to provide a custom HttpClient factory function when configuring KookSocketConfig, which is useful for scenarios requiring centralized HTTP client configuration, custom message handlers, or resilience policies.
Changes:
- Added a new overload of
DefaultRestClientProvider.Create()that acceptsFunc<HttpClient>to support factory-provided HttpClient instances - Modified
DefaultRestClientconstructor to accept an optionalhttpClientFactoryparameter and conditionally configure the HttpClient based on whether it's factory-provided - Updated the sample application to demonstrate HttpClient factory integration with
IHttpClientFactory
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Kook.Net.Rest/Net/DefaultRestClientProvider.cs | Adds new Create overload accepting Func<HttpClient> factory parameter with proper documentation |
| src/Kook.Net.Rest/Net/DefaultRestClient.cs | Modifies constructor to accept optional factory parameter and conditionally initializes HttpClient |
| samples/Kook.Net.Samples.TextCommands/Program.cs | Demonstrates HttpClient factory integration using IHttpClientFactory from Microsoft.Extensions.DependencyInjection |
Comments suppressed due to low confidence (1)
src/Kook.Net.Rest/Net/DefaultRestClient.cs:66
- HttpClient instances created by IHttpClientFactory should not be disposed. The factory manages the lifetime of HttpClients and their underlying handlers for connection pooling and proper resource management. Disposing a factory-created HttpClient can lead to resource leaks and connection pool issues.
You should track whether the HttpClient was created by the factory and conditionally dispose only when it was created directly. Consider adding a field like private readonly bool _shouldDisposeClient and setting it based on whether httpClientFactory was null, then only call _client.Dispose() in the Dispose method when _shouldDisposeClient is true.
if (httpClientFactory is not null)
_client = httpClientFactory();
else
{
_client = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
UseCookies = false,
UseProxy = useProxy,
Proxy = webProxy
});
SetHeader("accept-encoding", "gzip, deflate");
}
_cancellationToken = CancellationToken.None;
_serializerOptions = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
}
private void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
_client.Dispose();
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…arks and examples
…umentation to warn against setting BaseAddress
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.



No description provided.