@@ -22,8 +22,6 @@ public class RestClient : IRestClient
2222 ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy ( ) }
2323 } ;
2424
25- private HttpClient _httpClient ;
26-
2725 public RestClient ( ClientOptions options )
2826 {
2927 _options = MergeOptions ( options ) ;
@@ -36,7 +34,7 @@ public async Task<T> GetAsync<T>(
3634 JsonSerializerSettings serializerSettings = null ,
3735 CancellationToken cancellationToken = default )
3836 {
39- var response = await SendAsync ( uri , HttpMethod . Get , queryParams , headers ,
37+ using var response = await SendAsync ( uri , HttpMethod . Get , queryParams , headers ,
4038 cancellationToken : cancellationToken ) ;
4139
4240 return await response . ParseStreamAsync < T > ( serializerSettings ) ;
@@ -56,7 +54,7 @@ void AttachContent(HttpRequestMessage httpRequest)
5654 Encoding . UTF8 , "application/json" ) ;
5755 }
5856
59- var response = await SendAsync ( uri , HttpMethod . Post , queryParams , headers , AttachContent ,
57+ using var response = await SendAsync ( uri , HttpMethod . Post , queryParams , headers , AttachContent ,
6058 cancellationToken ) ;
6159
6260 return await response . ParseStreamAsync < T > ( serializerSettings ) ;
@@ -76,7 +74,7 @@ void AttachContent(HttpRequestMessage httpRequest)
7674 httpRequest . Content = new StringContent ( serializedBody , Encoding . UTF8 , "application/json" ) ;
7775 }
7876
79- var response = await SendAsync ( uri , new HttpMethod ( "PATCH" ) , queryParams , headers , AttachContent ,
77+ using var response = await SendAsync ( uri , new HttpMethod ( "PATCH" ) , queryParams , headers , AttachContent ,
8078 cancellationToken ) ;
8179
8280 return await response . ParseStreamAsync < T > ( serializerSettings ) ;
@@ -88,7 +86,7 @@ public async Task DeleteAsync(
8886 IDictionary < string , string > headers = null ,
8987 CancellationToken cancellationToken = default )
9088 {
91- await SendAsync ( uri , HttpMethod . Delete , queryParams , headers , null , cancellationToken ) ;
89+ using var _ = await SendAsync ( uri , HttpMethod . Delete , queryParams , headers , null , cancellationToken ) ;
9290 }
9391
9492 private static ClientOptions MergeOptions ( ClientOptions options )
@@ -141,7 +139,7 @@ private async Task<HttpResponseMessage> SendAsync(
141139 Action < HttpRequestMessage > attachContent = null ,
142140 CancellationToken cancellationToken = default )
143141 {
144- EnsureHttpClient ( ) ;
142+ using var client = GetHttpClient ( ) ;
145143
146144 requestUri = AddQueryString ( requestUri , queryParams ) ;
147145
@@ -156,7 +154,7 @@ private async Task<HttpResponseMessage> SendAsync(
156154
157155 attachContent ? . Invoke ( httpRequest ) ;
158156
159- var response = await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
157+ var response = await client . SendAsync ( httpRequest , cancellationToken ) ;
160158
161159 if ( ! response . IsSuccessStatusCode )
162160 {
@@ -174,17 +172,13 @@ private static void AddHeaders(HttpRequestMessage request, IDictionary<string, s
174172 }
175173 }
176174
177- private void EnsureHttpClient ( )
175+ private HttpClient GetHttpClient ( )
178176 {
179- if ( _httpClient != null )
180- {
181- return ;
182- }
183-
184177 var pipeline = new LoggingHandler { InnerHandler = new HttpClientHandler ( ) } ;
185178
186- _httpClient = new HttpClient ( pipeline ) ;
187- _httpClient . BaseAddress = new Uri ( _options . BaseUrl ) ;
179+ var httpClient = new HttpClient ( pipeline ) ;
180+ httpClient . BaseAddress = new Uri ( _options . BaseUrl ) ;
181+ return httpClient ;
188182 }
189183
190184 private static string AddQueryString ( string uri , IEnumerable < KeyValuePair < string , string > > queryParams )
0 commit comments