Skip to content

Commit 96764b3

Browse files
authored
Merge pull request #1130 from SamSmithNZ-dotcom/copilot/fix-stream-disposal-base-service-api-client
Fix resource disposal in BaseServiceApiClient
2 parents 5f22b50 + 0cb73e3 commit 96764b3

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/SamSmithNZ.Web/Services/BaseServiceAPIClient.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public void SetupClient(HttpClient client)
1818

1919
public async Task<List<T>> ReadMessageList<T>(Uri url)
2020
{
21-
HttpResponseMessage response = await _client.GetAsync(url);
21+
using HttpResponseMessage response = await _client.GetAsync(url);
2222
if (response.IsSuccessStatusCode == true)
2323
{
24-
Stream stream = await response.Content.ReadAsStreamAsync();
24+
await using Stream stream = await response.Content.ReadAsStreamAsync();
2525
if (stream != null && stream.Length > 0)
2626
{
2727
return await JsonSerializer.DeserializeAsync<List<T>>(stream);
@@ -40,10 +40,10 @@ public async Task<List<T>> ReadMessageList<T>(Uri url)
4040

4141
public async Task<T> ReadMessageItem<T>(Uri url)
4242
{
43-
HttpResponseMessage response = await _client.GetAsync(url);
43+
using HttpResponseMessage response = await _client.GetAsync(url);
4444
if (response.IsSuccessStatusCode == true)
4545
{
46-
Stream stream = await response.Content.ReadAsStreamAsync();
46+
await using Stream stream = await response.Content.ReadAsStreamAsync();
4747
if (stream != null && stream.Length > 0)
4848
{
4949
return await JsonSerializer.DeserializeAsync<T>(stream);
@@ -65,8 +65,8 @@ public async Task<T> ReadMessageItem<T>(Uri url)
6565
public async Task<bool> SaveMessageItem<T>(Uri url, T obj)
6666
{
6767
string jsonInString = JsonSerializer.Serialize(obj);
68-
StringContent content = new(jsonInString, Encoding.UTF8, "application/json");
69-
HttpResponseMessage response = await _client.PostAsync(url, content);
68+
using StringContent content = new(jsonInString, Encoding.UTF8, "application/json");
69+
using HttpResponseMessage response = await _client.PostAsync(url, content);
7070
if (response.IsSuccessStatusCode == true)
7171
{
7272
return true;
@@ -84,8 +84,8 @@ public async Task<T> SaveAndReturnMessageItem<T>(Uri url, T obj)
8484
{
8585
T result = default;
8686
string jsonInString = JsonSerializer.Serialize(obj);
87-
StringContent content = new(jsonInString, Encoding.UTF8, "application/json");
88-
HttpResponseMessage response = await _client.PostAsync(url, content);
87+
using StringContent content = new(jsonInString, Encoding.UTF8, "application/json");
88+
using HttpResponseMessage response = await _client.PostAsync(url, content);
8989
if (response.IsSuccessStatusCode == true)
9090
{
9191
string responseString = await response.Content.ReadAsStringAsync();
@@ -101,10 +101,10 @@ public async Task<T> SaveAndReturnMessageItem<T>(Uri url, T obj)
101101
//The type, R, is different than T. For example, if T is an Album, R is typically a string or int.
102102
public async Task<R> GetMessageScalar<R>(Uri url)
103103
{
104-
HttpResponseMessage response = await _client.GetAsync(url);
104+
using HttpResponseMessage response = await _client.GetAsync(url);
105105
if (response.IsSuccessStatusCode == true)
106106
{
107-
Stream stream = await response.Content.ReadAsStreamAsync();
107+
await using Stream stream = await response.Content.ReadAsStreamAsync();
108108
if (stream != null && stream.Length > 0)
109109
{
110110
return await JsonSerializer.DeserializeAsync<R>(stream);

0 commit comments

Comments
 (0)