Argument to dispose inner http client

This commit is contained in:
2024-09-06 23:44:27 +02:00
parent 2b23665145
commit 535a2cedc0
3 changed files with 18 additions and 7 deletions
@@ -8,7 +8,7 @@
<RootNamespace>System</RootNamespace>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Version>1.6.6</Version>
<Version>1.6.7</Version>
<Authors>Alexandru Macocian</Authors>
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
<Description>Extensions for the System namespace</Description>
@@ -8,6 +8,7 @@ namespace System.Net.Http;
public sealed class HttpClient<Tscope> : IHttpClient<Tscope>, IDisposable
{
private readonly bool disposeInnerClient = true;
private readonly HttpClient httpClient;
private readonly Type scope;
private EventHandler<HttpClientEventMessage> eventEmitted;
@@ -31,9 +32,10 @@ public sealed class HttpClient<Tscope> : IHttpClient<Tscope>, 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<Tscope> : IHttpClient<Tscope>, 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<Tscope> : IHttpClient<Tscope>, 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<Tscope> : IHttpClient<Tscope>, 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<Tscope> : IHttpClient<Tscope>, IDisposable
}
public void Dispose()
{
this.httpClient.Dispose();
if (this.disposeInnerClient)
{
this.httpClient.Dispose();
}
}
private void LogInformation(string url, string method)
@@ -7,7 +7,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RootNamespace>System</RootNamespace>
<Version>1.6.6</Version>
<Version>1.6.7</Version>
<Authors>Alexandru Macocian</Authors>
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
<Description>Extensions for the System namespace</Description>