From 535a2cedc009d18c6c7e06ba5791bb2e0a610db8 Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Fri, 6 Sep 2024 23:44:27 +0200 Subject: [PATCH] Argument to dispose inner http client --- .../SystemExtensions.NetCore.csproj | 2 +- .../Http/HttpClient.cs | 21 ++++++++++++++----- .../SystemExtensions.NetStandard.csproj | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj b/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj index 8238e34..ebc91b2 100644 --- a/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj +++ b/SystemExtensions.NetCore/SystemExtensions.NetCore.csproj @@ -8,7 +8,7 @@ System LICENSE true - 1.6.6 + 1.6.7 Alexandru Macocian https://github.com/AlexMacocian/SystemExtensions Extensions for the System namespace diff --git a/SystemExtensions.NetStandard/Http/HttpClient.cs b/SystemExtensions.NetStandard/Http/HttpClient.cs index 8bbc872..142d585 100644 --- a/SystemExtensions.NetStandard/Http/HttpClient.cs +++ b/SystemExtensions.NetStandard/Http/HttpClient.cs @@ -8,6 +8,7 @@ namespace System.Net.Http; public sealed class HttpClient : IHttpClient, IDisposable { + private readonly bool disposeInnerClient = true; private readonly HttpClient httpClient; private readonly Type scope; private EventHandler eventEmitted; @@ -31,9 +32,10 @@ public sealed class HttpClient : IHttpClient, IDisposable public TimeSpan Timeout { get => this.httpClient.Timeout; set => this.httpClient.Timeout = value; } #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. - public HttpClient() + public HttpClient(bool disposeInnerClient = true) #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. { + this.disposeInnerClient = disposeInnerClient; this.httpClient = new HttpClient(); this.scope = typeof(Tscope); } @@ -41,8 +43,10 @@ public sealed class HttpClient : IHttpClient, IDisposable #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. public HttpClient( #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. - HttpMessageHandler handler) + HttpMessageHandler handler, + bool disposeInnerClient = true) { + this.disposeInnerClient = disposeInnerClient; this.httpClient = new HttpClient(handler); this.scope = typeof(Tscope); } @@ -51,8 +55,10 @@ public sealed class HttpClient : IHttpClient, IDisposable public HttpClient( #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. HttpMessageHandler handler, - bool disposeHandler) + bool disposeHandler, + bool disposeInnerClient = true) { + this.disposeInnerClient = disposeInnerClient; this.httpClient = new HttpClient(handler, disposeHandler); this.scope = typeof(Tscope); } @@ -60,8 +66,10 @@ public sealed class HttpClient : IHttpClient, IDisposable #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. public HttpClient( #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. - HttpClient httpClient) + HttpClient httpClient, + bool disposeInnerClient = true) { + this.disposeInnerClient = disposeInnerClient; this.httpClient = httpClient.ThrowIfNull(nameof(httpClient)); this.scope = typeof(Tscope); } @@ -223,7 +231,10 @@ public sealed class HttpClient : IHttpClient, IDisposable } public void Dispose() { - this.httpClient.Dispose(); + if (this.disposeInnerClient) + { + this.httpClient.Dispose(); + } } private void LogInformation(string url, string method) diff --git a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj index 40dd059..0347364 100644 --- a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj +++ b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj @@ -7,7 +7,7 @@ LICENSE true System - 1.6.6 + 1.6.7 Alexandru Macocian https://github.com/AlexMacocian/SystemExtensions Extensions for the System namespace