Skip to content

Commit 29831a1

Browse files
Dispose
1 parent 8af1235 commit 29831a1

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

EssentialCSharp.Web.Tests/Integration/CaptchaTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,22 @@ public static ServiceProvider CreateServiceProvider()
4343
}
4444
public void Dispose()
4545
{
46-
ServiceProvider.Dispose();
46+
Dispose(disposing: true);
4747
GC.SuppressFinalize(this);
4848
}
4949

50+
protected virtual void Dispose(bool disposing)
51+
{
52+
if (disposing)
53+
{
54+
ServiceProvider.Dispose();
55+
}
56+
}
57+
5058
public async ValueTask DisposeAsync()
5159
{
52-
await ServiceProvider.DisposeAsync();
60+
await ServiceProvider.DisposeAsync().ConfigureAwait(false);
61+
Dispose(disposing: false);
5362
GC.SuppressFinalize(this);
5463
}
5564
}

EssentialCSharp.Web.Tests/WebApplicationFactory.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ public void InServiceScope(Action<IServiceProvider> action)
105105
action(scope.ServiceProvider);
106106
}
107107

108+
public override async ValueTask DisposeAsync()
109+
{
110+
await base.DisposeAsync().ConfigureAwait(false);
111+
if (_Connection != null)
112+
{
113+
await _Connection.DisposeAsync().ConfigureAwait(false);
114+
_Connection = null;
115+
}
116+
GC.SuppressFinalize(this);
117+
}
118+
108119
protected override void Dispose(bool disposing)
109120
{
110121
base.Dispose(disposing);

EssentialCSharp.Web/Services/CaptchaService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ public class CaptchaService(IHttpClientFactory clientFactory, IOptions<CaptchaOp
4141
HttpClient client = ClientFactory.CreateClient("hCaptcha");
4242

4343
// request api
44-
HttpResponseMessage res = await client.PostAsync(
45-
// base url is given in IHttpClientFactory service registration
46-
// hCaptcha wants URL-encoded POST
47-
"/siteverify", new FormUrlEncodedContent(postData), cancellationToken);
44+
// hCaptcha wants URL-encoded POST; base url is given in IHttpClientFactory service registration
45+
using FormUrlEncodedContent content = new(postData);
46+
HttpResponseMessage res = await client.PostAsync("/siteverify", content, cancellationToken);
4847

4948
res.EnsureSuccessStatusCode();
5049
// convert JSON string into Class

0 commit comments

Comments
 (0)