Skip to content

Commit 54cda1d

Browse files
Jabenclaude
andcommitted
Fix resource disposal and add CancellationToken.None to all CopyToAsync calls
Resource disposal fixes: - Wrapped HttpClientHandler, BasicAuthHandler, and HttpClient in using declarations - Ensures proper disposal of all HTTP-related resources when method exits - Prevents resource leaks in non-DI examples CancellationToken consistency: - Added explicit CancellationToken.None parameter to all CopyToAsync calls - Ensures consistent cancellation token usage across all examples - Removed .ConfigureAwait(false) in favor of explicit cancellation token Files updated: - DIExample: Added using System.Threading, CancellationToken.None - HtmlConvert: Added using declarations, CancellationToken.None - HtmlWithMarkdown: Added using declarations, CancellationToken.None - OfficeMerge: Added using declarations, CancellationToken.None (removed ConfigureAwait) - PdfMerge: Added using declarations (already had CancellationToken.None) - PdfConvert: Added using declarations (already had CancellationToken.None) - UrlConvert: Added using declarations, CancellationToken.None - Webhook: Added using declarations - UrlsToMergedPdf: Added using declarations, CancellationToken.None All 9 examples build successfully with 0 errors and 0 warnings. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 28bb8fd commit 54cda1d

9 files changed

Lines changed: 76 additions & 61 deletions

File tree

examples/DIExample/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Threading;
12
using Gotenberg.Sharp.API.Client;
23
using Gotenberg.Sharp.API.Client.Domain.Builders;
34
using Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;
@@ -30,7 +31,7 @@
3031

3132
using (var destinationStream = File.Create(resultPath))
3233
{
33-
await response.CopyToAsync(destinationStream);
34+
await response.CopyToAsync(destinationStream, CancellationToken.None);
3435
}
3536

3637
Console.WriteLine($"PDF created: {resultPath}");

examples/HtmlConvert/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323

2424
static async Task<string> CreateFromHtml(string destinationDirectory, string resourcePath, GotenbergSharpClientOptions options)
2525
{
26-
var handler = new HttpClientHandler();
27-
var httpClient = new HttpClient(
28-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
29-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
30-
: handler
31-
)
32-
{ BaseAddress = options.ServiceUrl };
26+
using var handler = new HttpClientHandler();
27+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
28+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
29+
: null;
30+
31+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
32+
{
33+
BaseAddress = options.ServiceUrl
34+
};
3335

3436
var sharpClient = new GotenbergSharpClient(httpClient);
3537

@@ -53,7 +55,7 @@ static async Task<string> CreateFromHtml(string destinationDirectory, string res
5355

5456
using (var destinationStream = File.Create(resultPath))
5557
{
56-
await response.CopyToAsync(destinationStream);
58+
await response.CopyToAsync(destinationStream, CancellationToken.None);
5759
}
5860

5961
return resultPath;

examples/HtmlWithMarkdown/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
static async Task<string> CreateFromMarkdown(string destinationDirectory, string resourcePath, GotenbergSharpClientOptions options)
2424
{
25-
var handler = new HttpClientHandler();
26-
var httpClient = new HttpClient(
27-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
28-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
29-
: handler
30-
)
31-
{ BaseAddress = options.ServiceUrl };
25+
using var handler = new HttpClientHandler();
26+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
27+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
28+
: null;
29+
30+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
31+
{
32+
BaseAddress = options.ServiceUrl
33+
};
3234

3335
var sharpClient = new GotenbergSharpClient(httpClient);
3436

@@ -56,7 +58,7 @@ static async Task<string> CreateFromMarkdown(string destinationDirectory, string
5658

5759
using (var destinationStream = File.Create(outPath))
5860
{
59-
await response.CopyToAsync(destinationStream);
61+
await response.CopyToAsync(destinationStream, CancellationToken.None);
6062
}
6163

6264
return outPath;

examples/OfficeMerge/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
static async Task<string> DoOfficeMerge(string sourceDirectory, string destinationDirectory, GotenbergSharpClientOptions options)
2424
{
25-
var handler = new HttpClientHandler();
26-
var httpClient = new HttpClient(
27-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
28-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
29-
: handler
30-
)
31-
{ BaseAddress = options.ServiceUrl };
25+
using var handler = new HttpClientHandler();
26+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
27+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
28+
: null;
29+
30+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
31+
{
32+
BaseAddress = options.ServiceUrl
33+
};
3234

3335
var client = new GotenbergSharpClient(httpClient);
3436

@@ -44,7 +46,7 @@ static async Task<string> DoOfficeMerge(string sourceDirectory, string destinati
4446

4547
using (var destinationStream = File.Create(mergeResultPath))
4648
{
47-
await response.CopyToAsync(destinationStream).ConfigureAwait(false);
49+
await response.CopyToAsync(destinationStream, CancellationToken.None);
4850
}
4951

5052
return mergeResultPath;

examples/PdfConvert/Program.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525

2626
static async Task<string> DoConversion(string sourcePath, string destinationPath, GotenbergSharpClientOptions options)
2727
{
28-
var handler = new HttpClientHandler();
29-
var httpClient = new HttpClient(
30-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
31-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
32-
: handler
33-
)
34-
{ BaseAddress = options.ServiceUrl };
28+
using var handler = new HttpClientHandler();
29+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
30+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
31+
: null;
32+
33+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
34+
{
35+
BaseAddress = options.ServiceUrl
36+
};
3537

3638
var sharpClient = new GotenbergSharpClient(httpClient);
3739

examples/PdfMerge/Program.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
static async Task<string> DoMerge(string sourcePath, string destinationPath, GotenbergSharpClientOptions options)
2424
{
25-
var handler = new HttpClientHandler();
26-
var httpClient = new HttpClient(
27-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
28-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
29-
: handler
30-
)
31-
{ BaseAddress = options.ServiceUrl };
25+
using var handler = new HttpClientHandler();
26+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
27+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
28+
: null;
29+
30+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
31+
{
32+
BaseAddress = options.ServiceUrl
33+
};
3234

3335
var sharpClient = new GotenbergSharpClient(httpClient);
3436

examples/UrlConvert/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525

2626
static async Task<string> CreateFromUrl(string destinationPath, string headerPath, string footerPath, GotenbergSharpClientOptions options)
2727
{
28-
var handler = new HttpClientHandler();
29-
var httpClient = new HttpClient(
30-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
31-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
32-
: handler
33-
)
34-
{ BaseAddress = options.ServiceUrl };
28+
using var handler = new HttpClientHandler();
29+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
30+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
31+
: null;
32+
33+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
34+
{
35+
BaseAddress = options.ServiceUrl
36+
};
3537

3638
var sharpClient = new GotenbergSharpClient(httpClient);
3739

@@ -57,7 +59,7 @@ static async Task<string> CreateFromUrl(string destinationPath, string headerPat
5759

5860
using (var destinationStream = File.Create(resultPath))
5961
{
60-
await response.CopyToAsync(destinationStream);
62+
await response.CopyToAsync(destinationStream, CancellationToken.None);
6163
}
6264

6365
return resultPath;

examples/UrlsToMergedPdf/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ static IEnumerable<UrlRequestBuilder> CreateRequestBuilders(IEnumerable<Uri> uri
6666

6767
static async Task<string> ExecuteRequestsAndMerge(IEnumerable<UrlRequest> requests, string destinationDirectory, GotenbergSharpClientOptions options)
6868
{
69-
var handler = new HttpClientHandler();
70-
var innerClient = new HttpClient(
71-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
72-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
73-
: handler
74-
)
69+
using var handler = new HttpClientHandler();
70+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
71+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
72+
: null;
73+
74+
using var innerClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
7575
{
7676
BaseAddress = options.ServiceUrl,
7777
Timeout = TimeSpan.FromMinutes(7)
@@ -101,7 +101,7 @@ static async Task<string> WriteFileAndGetPath(Stream responseStream, string dest
101101

102102
using (var destinationStream = File.Create(fullPath))
103103
{
104-
await responseStream.CopyToAsync(destinationStream);
104+
await responseStream.CopyToAsync(destinationStream, CancellationToken.None);
105105
}
106106
return fullPath;
107107
}

examples/Webhook/Program.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828

2929
static async Task CreateFromUrl(string headerPath, string footerPath, GotenbergSharpClientOptions options)
3030
{
31-
var handler = new HttpClientHandler();
32-
var httpClient = new HttpClient(
33-
!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
34-
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
35-
: handler
36-
)
37-
{ BaseAddress = options.ServiceUrl };
31+
using var handler = new HttpClientHandler();
32+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
33+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
34+
: null;
35+
36+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
37+
{
38+
BaseAddress = options.ServiceUrl
39+
};
3840

3941
var sharpClient = new GotenbergSharpClient(httpClient);
4042

0 commit comments

Comments
 (0)